SHED-Article2SOFTWARE-ITS-ON-OFF
Jump to navigation
Jump to search
ITS ON-OFF:
(tested-TK)
This software sketch uses an Input Sensor which is a pushbutton switch. If the button is pushed
it displays "ON" on the Serial Monitor and lights LED 13 on the Yourduino board. If the switch is not being pushed it displays "OFF". This shows simple Input Device sensing and simple decision making.
CONNECTIONS:
- Pushbutton Switch from +5 to Pin 3
- 10K Resistor from Pin 3 to ground
(Copy the text in the box below and Paste it into a blank Arduino IDE window)
/* SHED Magazine ITS_ON_OFF - Looks at Digital value of Pin 3 - Controls LED on Pin 13 - Sends to Serial Monitor ON or OFF - SEE the comments after "//" on each line below - CONNECTIONS: - 10K Pulldown Resistor from Pin 3 to Ground - Pushbutton switch from Pin 3 to +5V - V1.01 09/11/12 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ // None /*-----( Declare Constants and Pin Numbers )-----*/ #define switchPin 3 // NOTE: no ending ";" #define ledPin 13 /*-----( Declare objects )-----*/ // None /*-----( Declare Variables )-----*/ int switchState; // Switch was HIGH=1 or LOW=0 void setup() /****** SETUP: RUNS ONCE ******/ { pinMode(ledPin, OUTPUT); // This pin will be an output pinMode(switchPin, INPUT); // Not really needed: default Serial.begin(9600); // Start up the Serial Monitor Serial.println("SHED Magazine ITS ON OFF Test"); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { switchState = digitalRead(switchPin); /*--(NOTE!!! "==" means "compare equal" )--*/ if (switchState == HIGH) // Switch was pushed { Serial.println("ITS -ON - !"); digitalWrite(ledPin, HIGH); } else { Serial.println("ITS -OFF- !"); digitalWrite(ledPin, LOW); } delay(100); // Let the switch stop bouncing }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //*********( THE END )***********