Components and supplies
Arduino Mega 2560 Rev3
4x4 Matrix Membrane Keypad
5v 8 relay module
Arduino Nano
8 Digits LED Seven Segment Display
12V battery
Tools and machines
Soldering Station
drill, screwdriver, soldering iron
Apps and platforms
ChatGPT
Project description
Code
Slave
c
Slave code with relay start instruction for Mega.
1#include <Wire.h> 2 3#define RELAY_COUNT 8 4int relayPins[RELAY_COUNT] = {A0, A1, A2, A3, A4, A5, A6, A7}; 5 6int currentRelay = 0; 7unsigned long previousMillis = 0; 8const unsigned long relayDelay = 100; 9 10void setup() { 11 Serial.begin(9600); 12 Wire.begin(8); 13 Wire.onReceive(receiveEvent); 14 15 for (int i = 0; i < RELAY_COUNT; i++) { 16 pinMode(relayPins[i], OUTPUT); 17 digitalWrite(relayPins[i], LOW); 18 } 19} 20 21void loop() { 22 // Le code de l'esclave peut effectuer d'autres tâches ici si nécessaire 23} 24 25void receiveEvent() { 26 while (Wire.available()) { 27 byte command = Wire.read(); 28 29 Serial.print("Received command: "); 30 Serial.println(command, HEX); 31 32 if (command == 0xAA) { 33 activateRelays(); 34 } else { 35 // Gérer d'autres commandes si nécessaire 36 } 37 } 38} 39 40void activateRelays() { 41 unsigned long currentMillis = millis(); 42 43 if (currentMillis - previousMillis >= relayDelay) { 44 previousMillis = currentMillis; 45 46 if (currentRelay < RELAY_COUNT) { 47 digitalWrite(relayPins[currentRelay], HIGH); 48 currentRelay++; 49 } else { 50 // Réinitialiser pour la prochaine séquence 51 currentRelay = 0; 52 } 53 } 54}
Master
c
Timer code with bus instruction for Nano.
1#include <Wire.h> 2#include <Keypad.h> 3#include <LedControl.h> 4#include "I2CCommunication.h" 5 6const byte ROWS = 4; 7const byte COLS = 4; 8char keys[ROWS][COLS] = { 9 {'1', '2', '3', 'A'}, 10 {'4', '5', '6', 'B'}, 11 {'7', '8', '9', 'C'}, 12 {'*', '0', '#', 'D'} 13}; 14byte rowPins[ROWS] = {6, 7, 8, 9}; 15byte colPins[COLS] = {2, 3, 4, 5}; 16 17Keypad keypadObj = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 18LedControl lc = LedControl(10, 11, 12, 1); 19 20long csecondsLeft; 21int decrement = 3; 22bool explosed = false; 23bool defused = false; 24 25long hours = 0; 26long minutes = 0; 27long seconds = 0; 28long hundredths = 0; 29 30String code = ""; 31 32char keypadOutput[32] = {0}; 33byte playSound = A0; 34 35bool isProgrammingTime = false; 36String inputKeys = ""; 37String programmedTime = ""; 38bool isCountingDown = false; 39 40void setup() { 41 csecondsLeft = 0; 42 Serial.begin(9600); 43 lc.shutdown(0, false); 44 lc.setIntensity(0, 10); 45 lc.clearDisplay(0); 46 47 tone(playSound, 2000, 80); digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); delay(150); 48 tone(playSound, 2000, 80); digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); delay(150); 49 tone(playSound, 2000, 80); digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); delay(150); 50 51 lc.setChar(0,7,'-',false); lc.setChar(0,0,'-',false); delay(80); lc.clearDisplay(0); 52 lc.setChar(0,6,'-',false); lc.setChar(0,1,'-',false); delay(80); lc.clearDisplay(0); 53 lc.setChar(0,5,'-',false); lc.setChar(0,2,'-',false); delay(80); lc.clearDisplay(0); 54 lc.setChar(0,4,'-',false); lc.setChar(0,3,'-',false); delay(80); lc.clearDisplay(0); 55 lc.setChar(0,5,'-',false); lc.setChar(0,2,'-',false); delay(80); lc.clearDisplay(0); 56 lc.setChar(0,6,'-',false); lc.setChar(0,1,'-',false); delay(80); lc.clearDisplay(0); 57 lc.setChar(0,7,'-',false); lc.setChar(0,0,'-',false); delay(80); lc.clearDisplay(0); 58 59 pinMode(A0, OUTPUT); 60 pinMode(A1, OUTPUT); 61 pinMode(A3, OUTPUT); 62 pinMode(A6, OUTPUT); 63 pinMode(13, OUTPUT); 64 65 analogWrite(A0, 0); 66 67 Wire.begin(8); // Initialise la communication I2C 68} 69 70void updateDisplay() { 71 long remainingSeconds = csecondsLeft / 60; 72 hours = remainingSeconds / 3600; 73 remainingSeconds %= 3600; 74 minutes = remainingSeconds / 60; 75 seconds = remainingSeconds % 60; 76 hundredths = csecondsLeft % 60; 77 78 lc.setDigit(0, 7, hours / 10, false); 79 lc.setDigit(0, 6, hours % 10, false); 80 lc.setDigit(0, 5, minutes / 10, false); 81 lc.setDigit(0, 4, minutes % 10, false); 82 lc.setDigit(0, 3, seconds / 10, false); 83 lc.setDigit(0, 2, seconds % 10, false); 84 lc.setDigit(0, 1, hundredths / 10, false); 85 lc.setDigit(0, 0, hundredths % 10, false); 86} 87 88void programTime() { 89 if (inputKeys.length() >= 8) { 90 inputKeys = inputKeys.substring(0, 8); 91 programmedTime = inputKeys.substring(0, 2) + ":" + inputKeys.substring(2, 4) + ":" + inputKeys.substring(4, 6) + ":" + inputKeys.substring(6, 8); 92 93 Serial.print("inputKeys: "); 94 Serial.println(inputKeys); 95 Serial.print("programmedTime: "); 96 Serial.println(programmedTime); 97 98 lc.clearDisplay(0); 99 100 inputKeys = ""; 101 } 102} 103 104void startCountdown() { 105 Serial.println("Starting countdown..."); 106 107 if (programmedTime.length() == 11) { 108 hours = programmedTime.substring(0, 2).toInt(); 109 minutes = programmedTime.substring(3, 5).toInt(); 110 seconds = programmedTime.substring(6, 8).toInt(); 111 hundredths = programmedTime.substring(9, 11).toInt(); 112 113 csecondsLeft = ((hours * 3600) + (minutes * 60) + seconds) * 60 + hundredths; 114 } else { 115 Serial.println("Invalid programmed time. Please enter a time greater than zero."); 116 return; 117 } 118 119 updateDisplay(); 120 isCountingDown = true; 121} 122 123void loop() { 124 125 char key = keypadObj.getKey(); 126 127 if (key) { 128 Serial.print("Key Pressed: "); 129 Serial.println(key); 130 131 if (key >= '0' && key <= '9') { 132 int num = key - '0'; 133 int displayIndex = 7 - inputKeys.length(); 134 lc.setDigit(0, displayIndex, num, false); 135 inputKeys += key; 136 tone(playSound, 2000, 50); 137 } 138 139 if (!isProgrammingTime && key == 'A') { 140 Serial.println("Programming time..."); 141 isProgrammingTime = true; 142 inputKeys = ""; 143 programmedTime = ""; 144 lc.clearDisplay(0); 145 } else if (isProgrammingTime && key == 'D') { 146 isProgrammingTime = false; 147 programTime(); 148 Serial.print("Programmed Time: "); 149 Serial.println(programmedTime); 150 startCountdown(); 151 programmedTime = ""; 152 } else if (isCountingDown && key == 'B') { 153 Serial.println(code); 154 decrement = 0; 155 defused = true; 156 code = ""; 157 } else if (isCountingDown && key == 'D') { 158 Serial.println(code); 159 decrement = 3; 160 defused = false; 161 code = ""; 162 } else if (key == 'C') { 163 isProgrammingTime = false; 164 lc.setDigit(0, 7, 0, false); 165 lc.clearDisplay(0); 166 } 167 } 168 169 170 if (isCountingDown) { 171 delay(50); 172 csecondsLeft -= decrement; 173 174 if (csecondsLeft < 0) { 175 csecondsLeft = 0; 176 if (!explosed) { 177 sendI2CCommand(0xFF); // Envoyer une commande I2C à l'esclave 178 digitalWrite(13, HIGH); 179 digitalWrite(A1, HIGH); 180 digitalWrite(A3, HIGH); 181 digitalWrite(A6, HIGH); 182 explosed = true; 183 } 184 } 185 updateDisplay(); 186 if (csecondsLeft % 60 == 0) { 187 tone(playSound, 3236, 150); 188 if (csecondsLeft == 0) { 189 // Envoyer une nouvelle commande pour démarrer la séquence des relais 190 sendI2CCommand(0xAA); 191 } 192 } 193 } 194}
bus management file
cpp
I2C communication .h file for Master code.
1// I2CCommunication.h 2#ifndef I2C_COMMUNICATION_H 3#define I2C_COMMUNICATION_H 4 5#include <Wire.h> 6#include <Arduino.h> 7 8void sendI2CCommand(byte command); 9void checkConnection(); 10 11#endif // I2C_COMMUNICATION_H
bus management file
cpp
I2C communication .cpp file for Master code.
1// I2CCommunication.cpp 2#include "I2CCommunication.h" 3#include <Arduino.h> 4 5 6void sendI2CCommand(byte command) { 7 Wire.beginTransmission(8); // Adresse I2C de l'esclave 8 Wire.write(command); 9 Wire.endTransmission(); 10}
Comments
Only logged in users can leave comments
acidfurs
0 Followers
•0 Projects
Table of contents
Intro
0
0