char val;
int ledPin = 13;
int sensorPin = A2;
int sensorValue = 0;
int baud = 9600;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(baud);
establishContact();
FIGURE 4. Peripheral hardware components:
PowerSwitch II, CdS sensor, RC circuit, and Uno.
Arduino, the voltage across pin 13 to ground is used to
signal a PowerSwitch II ($29, Sparkfun.com) which
switches on 110 VAC whenever pin 13 is HIGH. Figure 4
shows the three peripheral hardware components.
The use of the PowerSwitch II is simply for flexibility. If
you read Part I of this series, you know that there are a
variety of second devices that can be controlled by the
interface. With the PowerSwitch II, you could plug in and
control just about any home appliance or light fixture,
from a table lamp to a blender or a toaster. Assuming we
keep it simple and use a lamp, the photoresistor will work
fine as a monitoring sensor. If you opt for a different
output device, then you’ll have to modify the sensor
electronics accordingly. Given the simplicity of the design,
that shouldn’t be an issue.
void loop(){
if (Serial.available() > 0)
{
val = Serial.read();
if(val == '1'){
digitalWrite(ledPin, HIGH);
delay(1000);
}
else
{ digitalWrite(ledPin, LOW);
}
sensorValue = analogRead(sensorPin)/50;
Serial.println(sensorValue);
delay(100);
}
}
void establishContact() {
while (Serial.available() <= 0){
Serial.println("A");
delay(300);
Working with both Processing and an Arduino
microcontroller lends itself to parallel software
development as shown in Figure 5. Assuming you have
the screen real estate, having both IDEs (integrated
development environments) and the executing game
visible simultaneously supports rapid development and
debugging. What’s more, because the languages are
essentially identical, you don’t need to rethink logic or
control construction from one IDE to the other.
That said, the Processing IDE is more advanced,
with little niceties such as line numbering.
Software
The main loop continually checks the serial
port. If it reads a “1” from the computer, then
pin 13 — which is connected to the onboard
LED — is set HIGH. The LED and relay within
the PowerSwitch II are energized. The lamp
connected to the PowerSwitch II is energized.
} LISTING 1. Arduino source code.
The program for the Arduino is shown in
Listing 1. For clarity, the code is uncommented
here. However, the source file at the article link
is heavily commented.
As you can see, the code is straightforward,
right out of the example files from the Arduino
IDE. After defining the variables, the setup
routine is run, which begins the serial
communications and calls the subroutine to
establish contact with the serial port on the
Mac/PC.
FIGURE 5. Software development environments, with Arduino IDE
(left) and Processing IDE (right). The purple startup screen is
running in the lower left.
48 April 2017