const int button = 11; // the number of the pushbutton pin const int LED = 10; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(LED, OUTPUT); // initialize the pushbutton pin as an input: pinMode(button, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(button); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(LED, HIGH); } else { // turn LED off: digitalWrite(LED, LOW); } }
int buttonpin = 2; int red = 9; int yellow = 7; int orange = 5; int buttonstate = 0; void setup(){ pinMode(red, OUTPUT); pinMode(buttonpin, INPUT); pinMode(yellow, OUTPUT); pinMode(buttonpin, INPUT); pinMode(orange, OUTPUT); pinMode(buttonpin, INPUT); } void loop() { buttonstate = digitalRead(buttonpin); if (buttonstate == HIGH) { digitalWrite(red, LOW); digitalWrite(yellow, HIGH); digitalWrite(orange, HIGH); } else { digitalWrite(red, HIGH); digitalWrite(yellow, LOW); digitalWrite(orange, LOW); } } |