stilenor

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

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