Saturday, June 4, 2016

No. 1 - Nick + Andrea's Magical Button


In this experiment, we used the button to act as a switch and activate the LED. Our code is as follows:

const int buttonPin = 7; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin int count = 3; int buttonState = 0; int lastState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState != lastState) { if (buttonState == HIGH) { count++; } else { } delay(10); } lastState = buttonState; Serial.println(count); if (count % 2 == 0) { digitalWrite(ledPin, HIGH); delay(50); digitalWrite(ledPin, LOW); delay(50); digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }

LOGIC:
The state at which it starts as is OFF, but IF the button is pressed, it turns on the blinking LED and when the button is pressed again, the light turns off.
We also later adjusted the speed of the blinking:

No comments:

Post a Comment