
The first prototype with Arduino

The second prototype with Arduino
Book code
-----------------------------------------------------
int storyFirstSensor = 2;
int storySecondSensor = 3;
int storyThirdSensor = 4;
int storyFourthSensor = 5;
int storyFirstVal = 0;
int storySecondVal = 0;
int storyThridVal = 0;
int storyFourthVal = 0;
int roomFirstOutput = 6; // First room
int roomSecondOutput = 7; // Seond room
int roomThridOutput = 8; // Third room
int roomFourthOutput = 12; // Fourth room
void setup()
{
Serial.begin(9600); // use the serial port to send the values back to the computer
pinMode(storyFirstSensor,INPUT);
pinMode(storySecondSensor,INPUT);
pinMode(storyThirdSensor,INPUT);
pinMode(storyFourthSensor,INPUT);
pinMode(roomFirstOutput,OUTPUT);
pinMode(roomSecondOutput,OUTPUT);
pinMode(roomThridOutput,OUTPUT);
pinMode(roomFourthOutput,OUTPUT);
}
void loop()
{
storyFirstVal = digitalRead(storyFirstSensor);
storySecondVal = digitalRead(storySecondSensor);
storyThridVal = digitalRead(storyThirdSensor);
storyFourthVal = digitalRead(storyFourthSensor);
if (storyFirstVal == LOW) {
digitalWrite(roomFirstOutput, HIGH);
digitalWrite(roomSecondOutput, LOW);
digitalWrite(roomThridOutput, LOW);
digitalWrite(roomFourthOutput, LOW);
Serial.println ("Turning on light in the First Room");
}
else if (storySecondVal == LOW) {
digitalWrite(roomFirstOutput, LOW);
digitalWrite(roomSecondOutput, HIGH);
digitalWrite(roomThridOutput, LOW);
digitalWrite(roomFourthOutput, LOW);
Serial.println("Turning on light in the Second Room");
}
else if (storyThridVal == LOW) {
digitalWrite(roomFirstOutput, LOW);
digitalWrite(roomSecondOutput, LOW);
digitalWrite(roomThridOutput, HIGH);
digitalWrite(roomFourthOutput, LOW);
Serial.println("Turning on light in the Third Room");
}
else if (storyFourthVal == LOW) {
digitalWrite(roomFirstOutput, LOW);
digitalWrite(roomSecondOutput, LOW);
digitalWrite(roomThridOutput, LOW);
digitalWrite(roomFourthOutput, HIGH);
Serial.println("Turning on light in the Fourth Room");
}
else {
digitalWrite(roomFirstOutput, LOW);
digitalWrite(roomSecondOutput, LOW);
digitalWrite(roomThridOutput, LOW);
digitalWrite(roomFourthOutput, LOW);
Serial.println("Turning off & on light in all Rooms");
}
}
Sun's Room code
-----------------------------------------------------
//the H bridge takes two inputs from the Arduino to control the motor.
int motorPin0 = 2;
int motorPin1 = 3;
int enPin = 7; // enable pin
// there is one switch
int switchPin = 5;
int ledPin=6;
//declare the state variable
int state = 0;
void setup() {
//the motor control wires are outputs
pinMode(motorPin0, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(ledPin, OUTPUT);
//the switch is an input
pinMode(switchPin, INPUT);
}
void loop() {
//read the switch
state = digitalRead(switchPin);
//based on the state of the switch alternate the control pins to change he direction of the motor.
switch (state){
case 0:
digitalWrite(enPin, LOW);
digitalWrite(motorPin0, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(ledPin, LOW);
break;
case 1:
digitalWrite(ledPin, HIGH);
digitalWrite(enPin, HIGH);
digitalWrite(motorPin0, LOW);
digitalWrite(motorPin1, HIGH);
delay(300);
digitalWrite(enPin, LOW);
delay(1000);
digitalWrite(enPin, HIGH);
digitalWrite(motorPin0, HIGH);
digitalWrite(motorPin1, LOW);
delay(300);
break;
}
}