With the Arduino AND Logic Gate built on the MakerShield, it is time to upload the sketch. Example 7-1 operates the green LED using a pushbutton switch and a photocell. Here are the steps you’ll need to take:
- Attach the Arduino to your computer using a USB cable.
- Open the Arduino software and type Example 7-1 into the software’s text editor.
- Upload the sketch to the Arduino.
- Press the mini pushbutton switch for a moment.
The Arduino OR Logic Gate will turn on the LED when the photocell is covered or the pushbutton switch is pressed. Releasing the pushbutton switch or placing a light on the photocell turns off the LED, because an OR condition (in which either switch is closed) no longer exists.
The Arduino does this by using the ||
operator in the if statement. ||
is the computer programming symbol for the logical OR function.
Example 7-1. The Arduino OR Logic Gate sketch
/*
The Arduino OR Logic Gate
Turns on an LED connected to digital
pin 7, when pressing either a pushbutton switch or covering a photocell
attached to pins 3 and 4.
27 Jan 2013
Revised 4 September 2013
by Don Wilcher
*/
// constants won't change; they're used here to
// set pin numbers:
int B = 3; // the number of the B pushbutton pin
int A = 4; // the number of the A pushbutton pin
const int Cout = 7; // the number of the LED pin
// variables will change:
int AStatus = 0; // variable for reading the A pushbutton status
int BStatus = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(Cout, OUTPUT);
// initialize the pushbutton pins as inputs:
pinMode(B, INPUT);
pinMode(A, INPUT);
}
void loop(){
// read the state of the pushbutton value:
AStatus = digitalRead(A);
BStatus = digitalRead(B);
// check if the pushbuttons are pressed
// if it is, the buttonStatus is HIGH:
if (AStatus == HIGH || BStatus ==HIGH) {
// turn LED on:
digitalWrite(Cout, HIGH);
}
else {
// turn LED off:
digitalWrite(Cout, LOW);
}
}
After uploading the Arduino OR Logic Gate sketch to the Arduino microcontroller, the green LED is off. Pressing the pushbutton switch or placing your hand over the photocell will turn on the green LED. To completely test the Arduino OR Logic Gate’s operation, remember to use the TT shown in Figure 7-5.
The block diagram in Figure 7-8 shows the building blocks and the electrical signal flow for the Arduino OR Logic Gate. Circuit schematic diagrams are used by electrical engineers to quickly build cool electronic devices. The equivalent circuit schematic diagram for the Arduino OR Logic Gate is shown in Figure 7-9.
Figure 7-8. The Arduino OR Logic Gate block diagram
Figure 7-9. The Arduino OR Logic Gate circuit schematic diagram
TROUBLESHOOTING TIP
If the LED doesn’t light up after uploading the sketch to the Arduino microcontroller, check to see if it is connected to the correct output pin. Also, check to see if the LED has been connected in the proper orientation, with the short LED wire connected to GND.
Leave a Reply