Skip to content

Instantly share code, notes, and snippets.

@Protoneer
Last active August 29, 2015 14:09
Show Gist options
  • Save Protoneer/5c4640741a55f02895d5 to your computer and use it in GitHub Desktop.
Save Protoneer/5c4640741a55f02895d5 to your computer and use it in GitHub Desktop.
PWM for led control. With pot and buttons to adjust brightness
// All the pins needed
int pin_led = 9;
int pin_pot = 0;
int pin_button_mode = 2;
int pin_button_up = 3;
int pin_button_down = 4;
// Button state. 0 = button off , 1 = button pressed in
bool button_mode_state = 1;
bool button_up_state = 1;
bool button_down_state = 1;
// Store the current led brightness
int led_brightness_current = 0;
// Store both the pot and button brightness
int led_brightness_pot = 0;
int led_brightness_buttons = 0;
int led_button_change_size = 5; // Increase/decrease brightness by this amount
int mode = 0;
String serialReceiveData;
void read_pot_value()
{
led_brightness_pot = analogRead(pin_pot);
led_brightness_pot = (led_brightness_pot/4.2+10); // scale and shift the led_brightness_pot to be a useful
}
void read_button_states()
{
if(button_mode_state != digitalRead(pin_button_mode)){
button_mode_state != button_mode_state; // Invert old state
// Set Mode on button release
if(button_mode_state == 1)
{
mode++; // Increment
if(mode > 0 )
{
Serial.println("Mode : On");
}
}
// Loop through modes
if(mode > 1)
{
mode = 0;
Serial.println("Mode : Off");
}
delay(200);
}
if(button_up_state != digitalRead(pin_button_up) && mode != 0 ){
button_up_state != button_up_state;
// Increase Brightness
if(button_up_state == 1)
{
led_brightness_buttons += led_button_change_size;
}
if(led_brightness_buttons > 255)
{
led_brightness_buttons = 255;
}
}
if(button_down_state != digitalRead(pin_button_down) && mode != 0){
button_down_state != button_down_state;
// Decrease Brightness
if(button_down_state == 1)
{
led_brightness_buttons -= led_button_change_size;
}
if(led_brightness_buttons < 0)
{
led_brightness_buttons = 0;
}
}
}
void set_led_brightness(int brightness)
{
// Validate
if(brightness > 255)
{
brightness = 255;
}
if(brightness < 0)
{
brightness = 0;
}
if(led_brightness_current != brightness)
{
led_brightness_current = brightness;
analogWrite(pin_led,brightness);
Serial.print("Mode:");
Serial.print(mode);
Serial.print("Brightness:");
Serial.println(brightness);
delay(50);
}
}
bool read_serial()
{
bool result = false;
while (Serial.available() > 0)
{
char received = Serial.read();
if(isDigit(received)){
serialReceiveData += received;
}
// Process message when new line character is recieved
if (received == '\n')
{
set_led_brightness(serialReceiveData.toInt());
result =true;
mode = -1;
serialReceiveData = ""; // Clear recieved buffer
}
}
return result;
}
void setup()
{
// Set pin modes
pinMode(pin_led, OUTPUT);
pinMode(pin_button_mode, INPUT);
pinMode(pin_button_up, INPUT);
pinMode(pin_button_down, INPUT);
// start serial
Serial.begin(9600);
Serial.println("Starting Up...");
}
void loop()
{
if(!read_serial()) // If not set by serial command check hardware input
{
read_button_states();
read_pot_value();
// off mode
if(mode == 0)
{
set_led_brightness(0);
}
// button mode
if(mode == 1)
{
set_led_brightness(led_brightness_buttons);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment