Devices & Components
Arduino Uno Rev3
Tactile Switch, Top Actuated
Resistor 4.75k ohm
Breadboard (generic)
5 mm LED: Red
Software & Tools
Arduino IDE
Project description
Code
Switch Interface
arduino
1//CURIE PROJECTS 2const int ledPin1 = 13; // choose the pin for the LED1 3const int ledPin2=12; // choose the pin for the LED1 4const int inputPin1 = 9; // choose the input pin input1 5const int inputPin2=10; // choose the input pin input2 6int SW1State ; // Pullup resistor connected to GPIO 9 7int SW2State; // Pulldown resistor connected to GPIO 9 8int value1 = HIGH; // variable for reading the pin status 9int value2=LOW; 10 11void setup() 12{ 13 pinMode(ledPin1, OUTPUT); // declare LED1 as output 14 pinMode(ledPin2, OUTPUT); // declare LED2 as output 15 pinMode(inputPin1, INPUT); // declare switch1 as input 16 pinMode(inputPin2, INPUT); // declare switch2 as input 17} 18 19void loop() 20 { 21 value1 = digitalRead(inputPin1); // read input1 value 22 value2 = digitalRead(inputPin2); // read input2 value 23 if (value1== HIGH) 24 SW1State = HIGH; 25 if (value1 == LOW) 26 SW1State = LOW; 27 28 if (value2== HIGH) 29 SW2State = HIGH; 30 if (value2 == LOW) 31 SW2State = LOW; 32 if(SW1State==HIGH) 33 { 34 digitalWrite(ledPin1,HIGH); 35 } 36 else 37 { 38 digitalWrite(ledPin1,LOW); 39 40 } 41 42 if(SW2State==HIGH) 43 { 44 digitalWrite(ledPin2,HIGH); 45 } 46 else 47 { 48 digitalWrite(ledPin2,LOW); 49 50 } 51 52 }
Switch Interface
arduino
1//CURIE PROJECTS 2const int ledPin1 = 13; // choose the pin for the LED1 3const int ledPin2=12; // choose the pin for the LED1 4const int inputPin1 = 9; // choose the input pin input1 5const int inputPin2=10; // choose the input pin input2 6int SW1State ; // Pullup resistor connected to GPIO 9 7int SW2State; // Pulldown resistor connected to GPIO 9 8int value1 = HIGH; // variable for reading the pin status 9int value2=LOW; 10 11void setup() 12{ 13 pinMode(ledPin1, OUTPUT); // declare LED1 as output 14 pinMode(ledPin2, OUTPUT); // declare LED2 as output 15 pinMode(inputPin1, INPUT); // declare switch1 as input 16 pinMode(inputPin2, INPUT); // declare switch2 as input 17} 18 19void loop() 20 { 21 value1 = digitalRead(inputPin1); // read input1 value 22 value2 = digitalRead(inputPin2); // read input2 value 23 if (value1== HIGH) 24 SW1State = HIGH; 25 if (value1 == LOW) 26 SW1State = LOW; 27 28 if (value2== HIGH) 29 SW2State = HIGH; 30 if (value2 == LOW) 31 SW2State = LOW; 32 if(SW1State==HIGH) 33 { 34 digitalWrite(ledPin1,HIGH); 35 } 36 else 37 { 38 digitalWrite(ledPin1,LOW); 39 40 } 41 42 if(SW2State==HIGH) 43 { 44 digitalWrite(ledPin2,HIGH); 45 } 46 else 47 { 48 digitalWrite(ledPin2,LOW); 49 50 } 51 52 }
Downloadable files
Interfacing Switch
Interfacing Switch

Comments
Only logged in users can leave comments