Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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
- *
- */
- #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");
- }
- }
Advertisement