stilenor

Day 5 - Creative Day - Using Binary to control more lights

Nov 3rd, 2023
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.90 KB | None | 0 0
  1. /*
  2.  * 30 Days - Lost in Space
  3.  * Day 5 - Creative Day - Using Binary to control more lights
  4.  * with the same 3 switches
  5.  * have an "All Lights On" mode to catch any intruders
  6.  *
  7.  * Learn more at https://inventr.io/adventure
  8.  *
  9.  
  10.  */
  11.  
  12. #include "Arduino.h"  // include information about our HERO
  13.  
  14.  
  15.  
  16. const byte ENGINEROOM_LIGHTS_PIN = 7;    // pin controlling the engine room lights
  17. const byte AIRLOCK_LIGHTS_PIN = 8;  // pin controlling the airlock lights
  18. const byte BATHROOM_LIGHTS_PIN = 9;  // pin controlling the bathroom lights
  19. const byte CABIN_LIGHTS_PIN = 10;    // pin controlling the cabin lights
  20. const byte STORAGE_LIGHTS_PIN = 11;  // pin controlling the storage lights
  21. const byte COCKPIT_LIGHTS_PIN = 12;  // pin controlling the exterior lights
  22. int whichlight; //Store for Binary count
  23.  
  24.  
  25. // We will use a different input pin for each of our DIP switches and
  26. // we'll label them using binary state that switch represents.
  27.  
  28. const byte BINARY1_SWITCH_PIN = 2;    // cabin lights controlled by switch 1
  29. const byte BINARY2_SWITCH_PIN = 3;  // storage lights controlled by switch 2
  30. const byte BINARY4_SWITCH_PIN = 4;  // exterior lights controlled by switch 3
  31.  
  32. // the setup function runs once when you press reset or power the board
  33. void setup() {
  34.    Serial.begin(9600);  // Initialize Serial, set speed to 9600 bits/second (baud)
  35.  
  36.   // Configure our LED control pins as OUTPUT pins
  37.   pinMode(ENGINEROOM_LIGHTS_PIN, OUTPUT);    // pin controlling the engine room lights
  38.   pinMode(AIRLOCK_LIGHTS_PIN, OUTPUT);  // pin controlling the airlock lights
  39.   pinMode(BATHROOM_LIGHTS_PIN, OUTPUT);  // pin controlling the bathroom lights
  40.   pinMode(CABIN_LIGHTS_PIN, OUTPUT);    // pin controlling the cabin lights
  41.   pinMode(STORAGE_LIGHTS_PIN, OUTPUT);  // pin controlling the storage lights
  42.   pinMode(COCKPIT_LIGHTS_PIN, OUTPUT);  // pin controlling the exterior lights
  43.  
  44.   // Configure the switch pins as INPUT pins
  45.   pinMode(BINARY1_SWITCH_PIN, INPUT);    // pin connected to switch 1 (Binary count 1)
  46.   pinMode(BINARY2_SWITCH_PIN, INPUT);  // pin connected to switch 2 (Binary count 2)
  47.   pinMode(BINARY4_SWITCH_PIN, INPUT);  // pin connected to switch 3 (Binary count 4)
  48. }
  49.  
  50. // Each time through loop() we will check each switch in turn and set each light's
  51. // state appropriately.
  52. void loop() {
  53. whichlight = 0;
  54. // using individual if statements, aware there are other methods but this was simple and
  55. // easy to read
  56. if (digitalRead(BINARY1_SWITCH_PIN) == HIGH){
  57.   whichlight = whichlight + 1;
  58. }
  59. if (digitalRead(BINARY2_SWITCH_PIN) == HIGH){
  60.   whichlight = whichlight + 2;
  61. }
  62. if (digitalRead(BINARY4_SWITCH_PIN) == HIGH){
  63.   whichlight = whichlight + 4;
  64. }
  65.  
  66. // individual if statements again, switchcase would have probably been better here
  67. // but it's still easy to read
  68. if (whichlight == 0) {
  69.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  70.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  71.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  72.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  73.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  74.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  75.   Serial.print(whichlight);  // show whichlight value
  76.   Serial.println(", All Lights Off");
  77. }
  78. if (whichlight == 1) {
  79.   digitalWrite(ENGINEROOM_LIGHTS_PIN, HIGH);  // Engine Room lights on
  80.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  81.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  82.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  83.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  84.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  85.   Serial.print(whichlight);  // show whichlight value
  86.   Serial.println(", Engine Room Lights On");
  87. }
  88. if (whichlight == 2) {
  89.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  90.   digitalWrite(AIRLOCK_LIGHTS_PIN, HIGH);  // Airlock lights on
  91.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  92.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  93.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  94.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  95.   Serial.print(whichlight);  // show whichlight value
  96.   Serial.println(", Airlock Lights On");
  97. }
  98. if (whichlight == 3) {
  99.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  100.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  101.   digitalWrite(BATHROOM_LIGHTS_PIN, HIGH);  // Bathroom lights on
  102.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  103.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  104.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  105.   Serial.print(whichlight);  // show whichlight value
  106.   Serial.println(", Bathroom Lights Lights On");
  107. }
  108. if (whichlight == 4) {
  109.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  110.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  111.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  112.   digitalWrite(CABIN_LIGHTS_PIN, HIGH);  // Cabin lights on
  113.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  114.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  115.   Serial.print(whichlight);  // show whichlight value
  116.   Serial.println(", Cabin Lights On");
  117. }
  118. if (whichlight == 5) {
  119.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  120.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  121.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  122.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  123.   digitalWrite(STORAGE_LIGHTS_PIN, HIGH);  // Storage lights on
  124.   digitalWrite(COCKPIT_LIGHTS_PIN, LOW);  // Cockpit lights off
  125.   Serial.print(whichlight);  // show whichlight value
  126.   Serial.println(", Storage Lights On");
  127. }
  128. if (whichlight == 6) {
  129.   digitalWrite(ENGINEROOM_LIGHTS_PIN, LOW);  // Engine Room lights off
  130.   digitalWrite(AIRLOCK_LIGHTS_PIN, LOW);  // Airlock lights off
  131.   digitalWrite(BATHROOM_LIGHTS_PIN, LOW);  // Bathroom lights off
  132.   digitalWrite(CABIN_LIGHTS_PIN, LOW);  // Cabin lights off
  133.   digitalWrite(STORAGE_LIGHTS_PIN, LOW);  // Storage lights off
  134.   digitalWrite(COCKPIT_LIGHTS_PIN, HIGH);  // Cockpit lights on
  135.   Serial.print(whichlight);  // show whichlight value
  136.   Serial.println(", Cockpit Lights On");
  137. }
  138. if (whichlight == 7) { //had a spare result so decided to make it the opposite of all off (binary 0)
  139.   digitalWrite(ENGINEROOM_LIGHTS_PIN, HIGH);  // Engine Room lights on
  140.   digitalWrite(AIRLOCK_LIGHTS_PIN, HIGH);  // Airlock lights on
  141.   digitalWrite(BATHROOM_LIGHTS_PIN, HIGH);  // Bathroom lights on
  142.   digitalWrite(CABIN_LIGHTS_PIN, HIGH);  // Cabin lights on
  143.   digitalWrite(STORAGE_LIGHTS_PIN, HIGH);  // Storage lights on
  144.   digitalWrite(COCKPIT_LIGHTS_PIN, HIGH);  // Cockpit lights on
  145.   Serial.print(whichlight);  // show whichlight value
  146.   Serial.println(", All Lights On");
  147. }
  148.  
  149. }
Tags: inventr.io
Advertisement