DonutBlink: P-Comp lab 2
A step further
Blinking Donuts: When the pink donut is placed on the aluminum surface, the Red LED lights up; when the white donut is placed on the surface, only the Green LED lights up; when both are placed down, the Red and Green LED alternate blinking.
Simply two switches – one makes the yellow LED light up, one makes the Red LED light up; both switches on make the LED’s alternately blink
The problem is that because I am using delay() function, there is a 500 ms delay before the switches register. I am going to try to use the Arduino BlinkWithoutDelay example to rectify this.
Click here for the code and the rest of the lab
// declare variables:
int switchPinA = 2; // digital input pin for first switch A
int switchPinB = 5; //digital input pin for switch NB
int yellowLedPin = 3; // digital output pin for an LED
int redLedPin = 4; // digital output pin for an LED
int switchStateA = 0; // the state of the switch
int switchStateB = 0;void setup() {
pinMode(switchPinA, INPUT); // set the switch pin A to be an input
pinMode(switchPinB, INPUT); //set the switch pin B to be an input
pinMode(yellowLedPin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
}void loop() {
// read the switch input:
switchStateA = digitalRead(switchPinA);
switchStateB = digitalRead(switchPinB);if (switchStateA == 1 )
{
// if the switch is closed:
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
delay(500);
digitalWrite(redLedPin, LOW); // turn off the red LED
delay(500);
}
else {
// if the switch is open:
digitalWrite(yellowLedPin, LOW); //
digitalWrite(redLedPin, LOW); // turn off both LEDS
}if (switchStateB == 1) {
// if the switch is closed:
digitalWrite( redLedPin, HIGH); // turn off the yellow LED
delay(500);
digitalWrite(yellowLedPin, LOW); // turn on the red LED
delay(500);
}
else {
// if the switch is open:
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW); // turn off both
}
}
This is an LED blinking, it’s from Lesson 1 of the Arduino tutorial
Successful Arduino lab 2 using two wires as a switch