YourDuinoStarter ItsOnOff

From ArduinoInfo
Jump to navigation Jump to search

ITS ON-OFF:

Switch_LED-500.jpg
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".
Other devices such as Tilt Switch or Photoresistor can be used in place of the simple switch. This shows simple Input Device sensing and simple decision making.
CONNECTIONS:
  • Pushbutton Switch from +5 to Pin 3
  • OR Photoresistor from +5 to Pin 3
  • OR Tilt 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)
/* YourDuinoStarter Example: ItsOnOff
 - 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 )-----*/
const int switchPin = 3;
const int 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("YourDuinoStarterSet 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 )***********