/*
* 30 Days - Lost in Space
* Day 5 - Creative Day - Using Binary to control more lights
* with the same 3 switches
* have an "All Lights On" mode to catch any intruders
*
* Learn more at https://inventr.io/adventure
*
Binary counting:
Binary uses only 0's and 1's. Switches have either on or off so we can use them
for Binary counting.
If a switch is off the number would be 0 and if it's on the number would be 1
As we have 3 switches, Switch 1 = 1, Switch 2 = 2, and Switch 3 = 4
If Switch 1 and Switch 3 are in the on position the count would be 1 + 4 which = 5
If there were more switches, binary counting would see it as (if turned on):
Switch 1 = 1
Switch 2 = 2
Switch 3 = 4
Switch 4 = 8
Switch 5 = 16
Switch 6 = 32
Switch 7 = 64
Switch 8 = 128
If all were turned on it would be 1+2+4+8+16+32+64+128 which would = 255
*/
#include "Arduino.h" // include information about our HERO
const byte ENGINEROOM_LIGHTS_PIN = 7; // pin controlling the engine room lights
const byte AIRLOCK_LIGHTS_PIN = 8; // pin controlling the airlock lights
const byte BATHROOM_LIGHTS_PIN = 9; // pin controlling the bathroom lights
const byte CABIN_LIGHTS_PIN = 10; // pin controlling the cabin lights
const byte STORAGE_LIGHTS_PIN = 11; // pin controlling the storage lights
const byte COCKPIT_LIGHTS_PIN = 12; // pin controlling the exterior lights
int whichlight; //Store for Binary count
// We will use a different input pin for each of our DIP switches and
// we'll label them using binary state that switch represents.
const byte BINARY1_SWITCH_PIN = 2; // cabin lights controlled by switch 1
const byte BINARY2_SWITCH_PIN = 3; // storage lights controlled by switch 2
const byte BINARY4_SWITCH_PIN = 4; // exterior lights controlled by switch 3
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600); // Initialize Serial, set speed to 9600 bits/second (baud)
// Configure our LED control pins as OUTPUT pins
pinMode(ENGINEROOM_LIGHTS_PIN, OUTPUT); // pin controlling the engine room lights
pinMode(AIRLOCK_LIGHTS_PIN, OUTPUT); // pin controlling the airlock lights
pinMode(BATHROOM_LIGHTS_PIN, OUTPUT); // pin controlling the bathroom lights
pinMode(CABIN_LIGHTS_PIN, OUTPUT); // pin controlling the cabin lights
pinMode(STORAGE_LIGHTS_PIN, OUTPUT); // pin controlling the storage lights
pinMode(COCKPIT_LIGHTS_PIN, OUTPUT); // pin controlling the exterior lights
// Configure the switch pins as INPUT pins
pinMode(BINARY1_SWITCH_PIN, INPUT); // pin connected to switch 1 (Binary count 1)
pinMode(BINARY2_SWITCH_PIN, INPUT); // pin connected to switch 2 (Binary count 2)
pinMode(BINARY4_SWITCH_PIN, INPUT); // pin connected to switch 3 (Binary count 4)
}
// Each time through loop() we will check each switch in turn and set each light's
// state appropriately.
void loop() {
whichlight = 0;
// using individual if statements, aware there are other methods but this was simple and
// easy to read
if (digitalRead(BINARY1_SWITCH_PIN) == HIGH){
whichlight = whichlight + 1;
}
if (digitalRead(BINARY2_SWITCH_PIN) == HIGH){
whichlight = whichlight + 2;
}
if (digitalRead(BINARY4_SWITCH_PIN) == HIGH){
whichlight = whichlight + 4;
}
// individual if statements again, switchcase would have probably been better here
// but it's still easy to read
if (whichlight == 0) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", All Lights Off");
}
if (whichlight == 1) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, HIGH); // Engine Room lights on
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", Engine Room Lights On");
}
if (whichlight == 2) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, HIGH); // Airlock lights on
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", Airlock Lights On");
}
if (whichlight == 3) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, HIGH); // Bathroom lights on
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", Bathroom Lights Lights On");
}
if (whichlight == 4) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, HIGH); // Cabin lights on
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", Cabin Lights On");
}
if (whichlight == 5) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, HIGH); // Storage lights on
digitalWrite(COCKPIT_LIGHTS_PIN, LOW); // Cockpit lights off
Serial.print(whichlight); // show whichlight value
Serial.println(", Storage Lights On");
}
if (whichlight == 6) {
digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW); // Engine Room lights off
digitalWrite(AIRLOCK_LIGHTS_PIN, LOW); // Airlock lights off
digitalWrite(BATHROOM_LIGHTS_PIN, LOW); // Bathroom lights off
digitalWrite(CABIN_LIGHTS_PIN, LOW); // Cabin lights off
digitalWrite(STORAGE_LIGHTS_PIN, LOW); // Storage lights off
digitalWrite(COCKPIT_LIGHTS_PIN, HIGH); // Cockpit lights on
Serial.print(whichlight); // show whichlight value
Serial.println(", Cockpit Lights On");
}
if (whichlight == 7) { //had a spare result so decided to make it the opposite of all off (binary 0)
digitalWrite(ENGINEROOM_LIGHTS_PIN, HIGH); // Engine Room lights on
digitalWrite(AIRLOCK_LIGHTS_PIN, HIGH); // Airlock lights on
digitalWrite(BATHROOM_LIGHTS_PIN, HIGH); // Bathroom lights on
digitalWrite(CABIN_LIGHTS_PIN, HIGH); // Cabin lights on
digitalWrite(STORAGE_LIGHTS_PIN, HIGH); // Storage lights on
digitalWrite(COCKPIT_LIGHTS_PIN, HIGH); // Cockpit lights on
Serial.print(whichlight); // show whichlight value
Serial.println(", All Lights On");
}
}
Comments
0 B
|👍
/👎