Components and supplies
Pmod SD
9V battery (generic)
LED (generic)
Solderless Breadboard Half Size
RobotGeek Sensor Shield
Buzzer
Real Time Clock (RTC)
Arduino UNO
Jumper wires (generic)
Tools and machines
9V DC Battery Connector
Digilent Screwdriver
Apps and platforms
Arduino IDE
Project description
Code
SLAVE_CODE
arduino
To control Slave Arduino!
1/* SD card datalogger 2 3 This example shows how to log data from TEMPRATURE AND HUMIDITY SENSOR, 4 to an SD card using the SD library. 5 6 The circuit: 7 * analog sensors on analog ins 0, 1, and 2 8 * SD card attached to SPI bus as follows: 9 ** MOSI - pin 11 10 ** MISO - pin 12 11 ** CLK - pin 13 12 ** CS - pin 10 (for MKRZero SD: SDCARD_SS_PIN) 13 14by PIYUSH KUMAR SINGH 15 16 17 */ 18 19#include <SPI.h> 20#include <SD.h> 21 22#include "Wire.h" 23#define DS3231_I2C_ADDRESS 0x68 24 25// Convert normal decimal numbers to binary coded decimal 26 27int x; 28 29byte decToBcd(byte val) 30{ 31 return( (val/10*16) + (val%10) ); 32} 33// Convert binary coded decimal to normal decimal numbers 34byte bcdToDec(byte val) 35{ 36 return( (val/16*10) + (val%16) ); 37} 38 39 40 41 42 43 44 45 46const int chipSelect = 10; 47 48 49 50 51void setup() { 52 53 Serial.begin(9600); 54 Wire.begin(12); 55 56 Wire.onReceive(receiveEvent); 57 58 while (!Serial) { 59 ; // wait for serial port to connect. Needed for native USB port only 60 } 61 62 63 Serial.print("Initializing SD card..."); 64 65 66 // see if the card is present and can be initialized: 67 if (!SD.begin(chipSelect)) { 68 Serial.println("Card failed, or not present"); 69 // don't do anything more: 70 return; 71 } 72 Serial.println("card initialized."); 73} 74 75 76void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte 77dayOfMonth, byte month, byte year) 78{ 79 // sets time and date data to DS3231 80 Wire.beginTransmission(DS3231_I2C_ADDRESS); 81 Wire.write(0); // set next input to start at the seconds register 82 Wire.write(decToBcd(second)); // set seconds 83 Wire.write(decToBcd(minute)); // set minutes 84 Wire.write(decToBcd(hour)); // set hours 85 Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) 86 Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) 87 Wire.write(decToBcd(month)); // set month 88 Wire.write(decToBcd(year)); // set year (0 to 99) 89 Wire.endTransmission(); 90} 91 92 93 94 95 96 97void readDS3231time(byte *second, 98byte *minute, 99byte *hour, 100byte *dayOfWeek, 101byte *dayOfMonth, 102byte *month, 103byte *year) 104{ 105 Wire.beginTransmission(DS3231_I2C_ADDRESS); 106 Wire.write(0); // set DS3231 register pointer to 00h 107 Wire.endTransmission(); 108 Wire.requestFrom(DS3231_I2C_ADDRESS, 7); 109 // request seven bytes of data from DS3231 starting from register 00h 110 *second = bcdToDec(Wire.read() & 0x7f); 111 *minute = bcdToDec(Wire.read()); 112 *hour = bcdToDec(Wire.read() & 0x3f); 113 *dayOfWeek = bcdToDec(Wire.read()); 114 *dayOfMonth = bcdToDec(Wire.read()); 115 *month = bcdToDec(Wire.read()); 116 *year = bcdToDec(Wire.read()); 117} 118 119 120 121 122 123 124 125 126 127 128 129 130void writeTime() 131{ 132 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 133 // retrieve data from DS3231 134 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, 135 &year); 136 137 String TIMEString=""; 138 File TIMEFile = SD.open("IR.txt", FILE_WRITE); 139 140 141TIMEString += "TIME: "; 142TIMEString += String(hour, DEC); 143TIMEString += ":"; 144 145 146if (minute<10) 147 { 148 TIMEString += "0"; 149 150 } 151 152 TIMEString += String(minute, DEC); 153 154 TIMEString += ":"; 155 156 157 if (second<10) 158 { 159 TIMEString += "0"; 160 } 161 TIMEString += String(second, DEC); 162 163 164TIMEString += " "; 165 166TIMEString += String(dayOfMonth, DEC); 167TIMEString += "/"; 168TIMEString += String(month, DEC); 169 TIMEString += "/"; 170 TIMEString += String(year, DEC); 171 172 TIMEString +=" Day of week: "; 173 174 175 176 177 switch(dayOfWeek){ 178 case 1: 179 TIMEString +="Sunday"; 180 break; 181 case 2: 182 TIMEString +="Monday"; 183 break; 184 case 3: 185 TIMEString +="Tuesday"; 186 break; 187 case 4: 188 TIMEString +="Wednesday"; 189 break; 190 case 5: 191 TIMEString +="Thursday"; 192 break; 193 case 6: 194 TIMEString +="Friday"; 195 break; 196 case 7: 197 TIMEString +="Saturday"; 198 break; 199 } 200 201// if the file is available, write to it: 202 if (TIMEFile) { 203 TIMEFile.println(TIMEString); 204 TIMEFile.close(); 205 // print to the serial port too: 206 Serial.println(TIMEString); 207 } 208 // if the file isn't open, pop up an error: 209 else { 210 Serial.println("error opening IR.txt"); 211 } 212} 213 214void receiveEvent(int bytes) { 215 x = Wire.read(); // read one character from the I2C 216} 217 218 219 220 221 222void loop() { 223 String TIME = ""; 224 225 switch(x) 226 { 227 case 1: { writeTime(); 228 TIME += "SAA"; 229 break; 230 } 231 232 case 2: { writeTime(); 233 TIME += "RE"; 234 break; 235 } 236 237 case 3: { writeTime(); 238 TIME += "GAA"; 239 break; 240 } 241 242 case 4: { writeTime(); 243 TIME += "MAA"; 244 break; 245 } 246 247 case 5: { writeTime(); 248 TIME += "PAA"; 249 } 250 case 6: { writeTime(); 251 TIME += "DHA"; 252 break; 253 } 254 255 case 7: { writeTime(); 256 TIME += "NI"; 257 break; 258 } 259 260default : { writeTime(); 261 TIME += "empty"; 262 break; 263 } 264 265 266 } 267 268 269 File mainFile = SD.open("song.txt", FILE_WRITE); 270 271 272 // if the file is available, write to it: 273 if (mainFile) { 274 mainFile.println(TIME); 275 mainFile.close(); 276 // print to the serial port too: 277 Serial.println(TIME); 278 } 279 // if the file isn't open, pop up an error: 280 else { 281 Serial.println("error opening IR.txt"); 282 } 283 284 285} 286
SLAVE_CODE
arduino
To control Slave Arduino!
1/* SD card datalogger 2 3 This example shows how to log data from 4 TEMPRATURE AND HUMIDITY SENSOR, 5 to an SD card using the SD library. 6 7 8 The circuit: 9 * analog sensors on analog ins 0, 1, and 2 10 * SD card attached 11 to SPI bus as follows: 12 ** MOSI - pin 11 13 ** MISO - pin 12 14 ** CLK - pin 15 13 16 ** CS - pin 10 (for MKRZero SD: SDCARD_SS_PIN) 17 18by PIYUSH KUMAR SINGH 19 20 21 22 */ 23 24#include <SPI.h> 25#include <SD.h> 26 27#include "Wire.h" 28#define 29 DS3231_I2C_ADDRESS 0x68 30 31// Convert normal decimal numbers to binary coded 32 decimal 33 34int x; 35 36byte decToBcd(byte val) 37{ 38 return( (val/10*16) 39 + (val%10) ); 40} 41// Convert binary coded decimal to normal decimal numbers 42byte 43 bcdToDec(byte val) 44{ 45 return( (val/16*10) + (val%16) ); 46} 47 48 49 50 51 52 53 54 55const 56 int chipSelect = 10; 57 58 59 60 61void setup() { 62 63 Serial.begin(9600); 64 65 Wire.begin(12); 66 67 Wire.onReceive(receiveEvent); 68 69 while (!Serial) 70 { 71 ; // wait for serial port to connect. Needed for native USB port only 72 73 } 74 75 76 Serial.print("Initializing SD card..."); 77 78 79 // see 80 if the card is present and can be initialized: 81 if (!SD.begin(chipSelect)) { 82 83 Serial.println("Card failed, or not present"); 84 // don't do anything 85 more: 86 return; 87 } 88 Serial.println("card initialized."); 89} 90 91 92void 93 setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte 94dayOfMonth, 95 byte month, byte year) 96{ 97 // sets time and date data to DS3231 98 Wire.beginTransmission(DS3231_I2C_ADDRESS); 99 100 Wire.write(0); // set next input to start at the seconds register 101 Wire.write(decToBcd(second)); 102 // set seconds 103 Wire.write(decToBcd(minute)); // set minutes 104 Wire.write(decToBcd(hour)); 105 // set hours 106 Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 107 7=Saturday) 108 Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) 109 Wire.write(decToBcd(month)); 110 // set month 111 Wire.write(decToBcd(year)); // set year (0 to 99) 112 Wire.endTransmission(); 113} 114 115 116 117 118 119 120void 121 readDS3231time(byte *second, 122byte *minute, 123byte *hour, 124byte *dayOfWeek, 125byte 126 *dayOfMonth, 127byte *month, 128byte *year) 129{ 130 Wire.beginTransmission(DS3231_I2C_ADDRESS); 131 132 Wire.write(0); // set DS3231 register pointer to 00h 133 Wire.endTransmission(); 134 135 Wire.requestFrom(DS3231_I2C_ADDRESS, 7); 136 // request seven bytes of data from 137 DS3231 starting from register 00h 138 *second = bcdToDec(Wire.read() & 0x7f); 139 140 *minute = bcdToDec(Wire.read()); 141 *hour = bcdToDec(Wire.read() & 0x3f); 142 143 *dayOfWeek = bcdToDec(Wire.read()); 144 *dayOfMonth = bcdToDec(Wire.read()); 145 146 *month = bcdToDec(Wire.read()); 147 *year = bcdToDec(Wire.read()); 148} 149 150 151 152 153 154 155 156 157 158 159 160 161void 162 writeTime() 163{ 164 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 165 166 // retrieve data from DS3231 167 readDS3231time(&second, &minute, &hour, &dayOfWeek, 168 &dayOfMonth, &month, 169 &year); 170 171 String TIMEString=""; 172 File TIMEFile 173 = SD.open("IR.txt", FILE_WRITE); 174 175 176TIMEString += "TIME: "; 177TIMEString 178 += String(hour, DEC); 179TIMEString += ":"; 180 181 182if (minute<10) 183 { 184 185 TIMEString += "0"; 186 187 } 188 189 TIMEString += String(minute, DEC); 190 191 192 TIMEString += ":"; 193 194 195 if (second<10) 196 { 197 TIMEString += 198 "0"; 199 } 200 TIMEString += String(second, DEC); 201 202 203TIMEString += " 204 "; 205 206TIMEString += String(dayOfMonth, DEC); 207TIMEString += "/"; 208TIMEString 209 += String(month, DEC); 210 TIMEString += "/"; 211 TIMEString += String(year, 212 DEC); 213 214 TIMEString +=" Day of week: "; 215 216 217 218 219 switch(dayOfWeek){ 220 221 case 1: 222 TIMEString +="Sunday"; 223 break; 224 case 2: 225 TIMEString 226 +="Monday"; 227 break; 228 case 3: 229 TIMEString +="Tuesday"; 230 break; 231 232 case 4: 233 TIMEString +="Wednesday"; 234 break; 235 case 5: 236 TIMEString 237 +="Thursday"; 238 break; 239 case 6: 240 TIMEString +="Friday"; 241 break; 242 243 case 7: 244 TIMEString +="Saturday"; 245 break; 246 } 247 248// if the 249 file is available, write to it: 250 if (TIMEFile) { 251 TIMEFile.println(TIMEString); 252 253 TIMEFile.close(); 254 // print to the serial port too: 255 Serial.println(TIMEString); 256 257 } 258 // if the file isn't open, pop up an error: 259 else { 260 Serial.println("error 261 opening IR.txt"); 262 } 263} 264 265void receiveEvent(int bytes) { 266 x = Wire.read(); 267 // read one character from the I2C 268} 269 270 271 272 273 274void loop() { 275 276 String TIME = ""; 277 278 switch(x) 279 { 280 case 1: { writeTime(); 281 TIME 282 += "SAA"; 283 break; 284 } 285 286 case 2: { writeTime(); 287 288 TIME += "RE"; 289 break; 290 } 291 292 293 case 3: { writeTime(); 294 TIME += "GAA"; 295 break; 296 297 } 298 299 case 4: { writeTime(); 300 TIME += "MAA"; 301 302 break; 303 } 304 305 case 5: { writeTime(); 306 307 TIME += "PAA"; 308 } 309 case 6: { writeTime(); 310 311 TIME += "DHA"; 312 break; 313 } 314 315 316 case 7: { writeTime(); 317 TIME += "NI"; 318 break; 319 320 } 321 322default : { writeTime(); 323 TIME += "empty"; 324 325 break; 326 } 327 328 329 } 330 331 332 File mainFile 333 = SD.open("song.txt", FILE_WRITE); 334 335 336 // if the file is available, 337 write to it: 338 if (mainFile) { 339 mainFile.println(TIME); 340 mainFile.close(); 341 342 // print to the serial port too: 343 Serial.println(TIME); 344 } 345 // 346 if the file isn't open, pop up an error: 347 else { 348 Serial.println("error 349 opening IR.txt"); 350 } 351 352 353} 354
MASTER_CODE
arduino
To control Master Arduino!
1#include<Wire.h> 2 3 4 5const int ProxSensor1=8; 6const int ProxSensor2=9; 7const int ProxSensor3=10; 8 9int buzz=11; 10int i; 11int pin[8]={0,1,2,3,4,5,6,7}; 12 13 14 15 16void setup() { 17 18 Wire.begin(); 19 20 pinMode(buzz, OUTPUT); 21 22for(i=0;i<8;i++) 23{ pinMode(pin[i],OUTPUT); 24 } 25 26 27 pinMode(ProxSensor1,INPUT); 28 pinMode(ProxSensor2,INPUT); 29 pinMode(ProxSensor3,INPUT); 30 Serial.begin(9600); 31} 32 33void loop() { 34 35Wire.beginTransmission(12); 36 37 if((digitalRead(ProxSensor1)==HIGH) && (digitalRead(ProxSensor2)==LOW) && (digitalRead(ProxSensor3)==LOW)) { 38 tone(buzz,240,50); 39 pin[0]=HIGH; 40 pin[7]=LOW; 41 Serial.println("saa"); 42 Wire.write(1); 43 44 } 45 46 47 48 49 50 51 52 else if((digitalRead(ProxSensor1)==LOW) && (digitalRead(ProxSensor2)==HIGH) && (digitalRead(ProxSensor3)==LOW)) 53 { 54 tone(buzz,270,50); 55 pin[1]=HIGH; 56 pin[7]=LOW; 57 58 Serial.println("re"); //set the YELLOW LED on 59 Wire.write(2); 60 } 61 62 63 64 65 else if((digitalRead(ProxSensor1)==LOW) && (digitalRead(ProxSensor2)==LOW) && (digitalRead(ProxSensor3)==HIGH)) 66 { 67 tone(buzz,300,50); 68 pin[2]=HIGH; 69 pin[7]=LOW; 70 71 Serial.println("Ga"); 72 Wire.write(3); 73 } 74 75 76 else if((digitalRead(ProxSensor1)==HIGH) && (digitalRead(ProxSensor2)==HIGH) && (digitalRead(ProxSensor3)==LOW)) 77 { 78 tone(buzz,320,50); 79 pin[3]=HIGH; 80 pin[7]=LOW; 81 Serial.println("MA"); 82 Wire.write(4); 83 } 84 85 86 87 else if((digitalRead(ProxSensor1)==HIGH) && (digitalRead(ProxSensor2)==LOW) && (digitalRead(ProxSensor3)==HIGH)) 88 { 89 tone(buzz,360,50); 90 pin[4]=HIGH; 91 pin[7]=LOW; 92 Serial.println("PA"); 93 Wire.write(5); 94 } 95 96 97 98 else if((digitalRead(ProxSensor1)==LOW) && (digitalRead(ProxSensor2)==HIGH) && (digitalRead(ProxSensor3)==HIGH)) 99 { 100 tone(buzz,400,50); 101 pin[5]=HIGH; 102 pin[7]=LOW; 103 Serial.println("DHA"); 104 Wire.write(6); 105 } 106 107 else if((digitalRead(ProxSensor1)==HIGH) && (digitalRead(ProxSensor2)==HIGH) && (digitalRead(ProxSensor3)==HIGH)) 108 { 109 tone(buzz,450,50); 110 pin[6]=HIGH; 111 pin[7]=LOW; 112 Serial.println("NI"); 113 Wire.write(7); 114 } 115 116else{ 117 for(int i=0;i<8;i++) 118 { pin[i]=LOW; 119 } 120 pin[7]=HIGH; 121} 122 123} 124
Downloadable files
Master_Slave_Connection
To connect two Arduinos.
Master_Slave_Connection
Master_Slave_Connection
To connect two Arduinos.
Master_Slave_Connection
Comments
Only logged in users can leave comments
PIYUSH_K_SINGH
0 Followers
•0 Projects
Table of contents
Intro
0
0