Dimmer Switch


Warning: Undefined array key "Plum_Code_Box_code_2_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_2_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_3_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_3_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_4_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_4_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_5_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_5_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_6_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_6_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_7_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_7_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_8_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_8_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_9_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_9_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

Warning: Undefined array key "Plum_Code_Box_code_10_code" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 21

Warning: Undefined array key "Plum_Code_Box_code_10_type" in /var/www/codeshield.diyode.com/html/wp-content/plugins/plum-code-box/classes/Plum_Code_Box_Front.php on line 22

The switch turns the light on and off, the potentiometer controls the brightness.

Hardware used:
  • Switch
  • Potentiometer
  • LED
Code Used:
  • If statement
  • analogWrite
  • analogRead
  • digitalRead

#define ENCODER_A 14
#define ENCODER_B 15
#define ENCODER_PORT PINC
#define SWITCH 13
#define BUTTON 12
#define RGB_RED 11
#define RGB_GREEN 10
#define RGB_BLUE 9
#define LED 6
#define SERVO 5
#define PIEZO 3
#define RELAY 2
#define POT 2
#define HALL 3
#define THERMISTOR 4
#define PHOTOCELL 5
 
void setup () {
  pinMode(SWITCH, INPUT);
  pinMode(POT, INPUT);
  pinMode(LED, OUTPUT);
}
 
void loop () {
  // If the button is pressed
  if ( digitalRead( BUTTON ) == LOW ) {
    
    // Read the current state of the potentiometer
    int potVal = analogRead( POT );
    
    // Map the potentiometer's range of values to that of the led
    int ledVal = map( potVal, 0, 1023, 0, 255 );
    
    analogWrite( LED, ledVal );
    
  }
}
									

4 comments. Leave a Reply

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>