Components and supplies
Serial I2C LCD Backpack
TTP223B capacitive touch switch
2*16 LCD display
Piezo sounder
Arduino UNO
Deek robot data logging shield v1.0
Apps and platforms
Arduino IDE
Project description
Code
GymGeneral code
c_cpp
1/* 2 The circuit: 3 * SD card attached to SPI bus as follows: 4 ** MOSI - pin 11 5 ** MISO - pin 12 6 ** CLK - pin 13 7 ** CS - pin 10 -> for deek robot data shield v1.0 8 */ 9#include <SPI.h> 10#include <SD.h> 11 12const int chipSelect=10; 13 14//my_aliases 15const int down_switch=3; 16const int enter_switch=2; 17 18const int start_screen=1; 19const int initialization_screen=2; 20const int file_menu_screen=3; 21const int file_wont_open_screen=4; 22 23//my_boolean variables 24volatile bool count_updated=true; 25volatile bool enter_pressed=false; 26volatile bool beep_file_contents=false; 27volatile bool do_update=true; 28 29File root; 30File myFile; 31 32/*-----( Import needed libraries )-----*/ 33#include <Wire.h> // Comes with Arduino IDE 34// Get the LCD I2C Library here: 35// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads 36// Move any other LCD libraries to another folder or delete them 37// See Library "Docs" folder for possible commands etc. 38#include <LiquidCrystal_I2C.h> 39 40// set the LCD address to 0x27 for a 16 chars 2 line display 41// A FEW use address 0x3F ------------------------> like mine! 42// Set the pins on the I2C chip used for LCD connections: 43// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol 44LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address 45 46//my_global_variables 47int screen_number=start_screen; 48bool initialization_fail=false; 49bool file_exists=false; 50static String my_filename=""; 51 52const int sounder_pin=9;// the pin we attach our sounder to 53const int duty_cycle=128;//50% duty cycle for analogWrite() 54 55void setup() { 56 57//initial lcd setup 58 lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines 59 lcd.backlight(); //turn on backlight 60 lcd.clear(); 61 62 update_display(); 63 delay(5000); 64 screen_number=initialization_screen; 65 66//try to initialize sd card 67 if (!SD.begin(chipSelect)) { 68 initialization_fail=true; 69 update_display(); 70 return; 71 } 72 initialization_fail=false; 73 update_display(); 74 delay(5000); 75 76//for switches 77 pinMode(down_switch, INPUT);// sets the digital pin as input 78 pinMode(enter_switch, INPUT);// sets the digital pin as input 79 attachInterrupt(digitalPinToInterrupt(down_switch), enter_button, RISING); 80 attachInterrupt(digitalPinToInterrupt(enter_switch), increment_down, RISING); 81 82 // initialize digital pin as an output. 83 pinMode(sounder_pin, OUTPUT); 84 85}/*end of setup*/ 86 87void loop() { 88 89 root = SD.open("/"); 90 91 int repeats=0; 92 int frequency=0; 93 int on_time=0; 94 int off_time=0; 95 int index=0; 96 97while(1){ 98 99 if (do_update){do_update=false;update_display();} 100 101 if (count_updated){ 102 103 104 //cycle through the existing files on the SD card 105 count_updated=false; 106 107 File entry = root.openNextFile(); 108 if (! entry) { 109 // no more files -> so go back to beginning 110 entry.close(); 111 root.rewindDirectory(); 112 my_filename="No more"; 113 update_display(); 114 }else 115 { 116 my_filename=entry.name(); 117 screen_number=file_menu_screen; 118 update_display(); 119 entry.close();} 120 } 121 122//beep out the intervals from the chosed file 123if (beep_file_contents){ 124 125 beep_file_contents=false; 126 127 if (SD.exists(my_filename)) {file_exists=true;}else{file_exists=false;} 128 129 update_display(); 130 131 //read data from file 132 // re-open the file for reading: 133 myFile = SD.open(my_filename, FILE_READ); 134 if (myFile) { 135 136 // read from the file until there's nothing else in it: 137 while (myFile.available()) 138 { 139 repeats=myFile.parseInt(); 140 frequency=myFile.parseInt(); 141 on_time=myFile.parseInt(); 142 off_time=myFile.parseInt(); 143 144 for (index=0;index<repeats;index++) 145 { 146 if (frequency==1){TCCR1B = (TCCR1B & 0b11111000) | 0x04;}else{TCCR1B = (TCCR1B & 0b11111000) | 0x02;} 147 analogWrite(sounder_pin, duty_cycle); 148 delay(on_time*100); 149 analogWrite(sounder_pin,0); 150 delay(off_time*100); 151 } 152 } 153 154 // close the file: 155 myFile.close(); 156 } else { 157 // if the file didn't open, print an error: 158 screen_number=file_wont_open_screen; 159 update_display(); 160 } 161 162}//end beep out file 163 164} 165 166}/*end of loop*/ 167 168//my_functions 169 170//my_isr_functions 171void enter_button(void)//enter button 172{ switch(screen_number){case file_menu_screen:beep_file_contents=true;break; 173 case file_wont_open_screen:screen_number=file_menu_screen;do_update=true;break;} 174} 175 176void increment_down()//down button 177{if (screen_number==file_menu_screen){count_updated=true;}} 178 179//my_screen_functions 180void update_display(void) 181{ 182 switch (screen_number) 183 { 184 case start_screen:lcd.clear(); 185 lcd.setCursor(3,0); 186 lcd.print("GymGeneral"); 187 lcd.setCursor(3,1); 188 lcd.print("Gym timer"); 189 break; 190 case initialization_screen:lcd.clear(); 191 lcd.setCursor(0,0); 192 lcd.print("Init SD..."); 193 if (!initialization_fail){lcd.print("done.");}else{lcd.print("fail!");} 194 break; 195 case file_menu_screen:lcd.clear(); 196 lcd.setCursor(2,0); 197 lcd.print("Select file"); 198 lcd.setCursor(2,1); 199 lcd.print(" ");//clear any previous filename (8.3 character format, so 12 spaces) 200 lcd.setCursor(2,1); 201 lcd.print(my_filename); 202 break; 203 case file_wont_open_screen:lcd.clear(); 204 lcd.print("Can't open file"); 205 break; 206 } 207 } 208
GymGeneral code
c_cpp
1/* 2 The circuit: 3 * SD card attached to SPI bus as follows: 4 ** MOSI - pin 11 5 ** MISO - pin 12 6 ** CLK - pin 13 7 ** CS - pin 10 -> for deek robot data shield v1.0 8 */ 9#include <SPI.h> 10#include <SD.h> 11 12const int chipSelect=10; 13 14//my_aliases 15const int down_switch=3; 16const int enter_switch=2; 17 18const int start_screen=1; 19const int initialization_screen=2; 20const int file_menu_screen=3; 21const int file_wont_open_screen=4; 22 23//my_boolean variables 24volatile bool count_updated=true; 25volatile bool enter_pressed=false; 26volatile bool beep_file_contents=false; 27volatile bool do_update=true; 28 29File root; 30File myFile; 31 32/*-----( Import needed libraries )-----*/ 33#include <Wire.h> // Comes with Arduino IDE 34// Get the LCD I2C Library here: 35// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads 36// Move any other LCD libraries to another folder or delete them 37// See Library "Docs" folder for possible commands etc. 38#include <LiquidCrystal_I2C.h> 39 40// set the LCD address to 0x27 for a 16 chars 2 line display 41// A FEW use address 0x3F ------------------------> like mine! 42// Set the pins on the I2C chip used for LCD connections: 43// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol 44LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address 45 46//my_global_variables 47int screen_number=start_screen; 48bool initialization_fail=false; 49bool file_exists=false; 50static String my_filename=""; 51 52const int sounder_pin=9;// the pin we attach our sounder to 53const int duty_cycle=128;//50% duty cycle for analogWrite() 54 55void setup() { 56 57//initial lcd setup 58 lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines 59 lcd.backlight(); //turn on backlight 60 lcd.clear(); 61 62 update_display(); 63 delay(5000); 64 screen_number=initialization_screen; 65 66//try to initialize sd card 67 if (!SD.begin(chipSelect)) { 68 initialization_fail=true; 69 update_display(); 70 return; 71 } 72 initialization_fail=false; 73 update_display(); 74 delay(5000); 75 76//for switches 77 pinMode(down_switch, INPUT);// sets the digital pin as input 78 pinMode(enter_switch, INPUT);// sets the digital pin as input 79 attachInterrupt(digitalPinToInterrupt(down_switch), enter_button, RISING); 80 attachInterrupt(digitalPinToInterrupt(enter_switch), increment_down, RISING); 81 82 // initialize digital pin as an output. 83 pinMode(sounder_pin, OUTPUT); 84 85}/*end of setup*/ 86 87void loop() { 88 89 root = SD.open("/"); 90 91 int repeats=0; 92 int frequency=0; 93 int on_time=0; 94 int off_time=0; 95 int index=0; 96 97while(1){ 98 99 if (do_update){do_update=false;update_display();} 100 101 if (count_updated){ 102 103 104 //cycle through the existing files on the SD card 105 count_updated=false; 106 107 File entry = root.openNextFile(); 108 if (! entry) { 109 // no more files -> so go back to beginning 110 entry.close(); 111 root.rewindDirectory(); 112 my_filename="No more"; 113 update_display(); 114 }else 115 { 116 my_filename=entry.name(); 117 screen_number=file_menu_screen; 118 update_display(); 119 entry.close();} 120 } 121 122//beep out the intervals from the chosed file 123if (beep_file_contents){ 124 125 beep_file_contents=false; 126 127 if (SD.exists(my_filename)) {file_exists=true;}else{file_exists=false;} 128 129 update_display(); 130 131 //read data from file 132 // re-open the file for reading: 133 myFile = SD.open(my_filename, FILE_READ); 134 if (myFile) { 135 136 // read from the file until there's nothing else in it: 137 while (myFile.available()) 138 { 139 repeats=myFile.parseInt(); 140 frequency=myFile.parseInt(); 141 on_time=myFile.parseInt(); 142 off_time=myFile.parseInt(); 143 144 for (index=0;index<repeats;index++) 145 { 146 if (frequency==1){TCCR1B = (TCCR1B & 0b11111000) | 0x04;}else{TCCR1B = (TCCR1B & 0b11111000) | 0x02;} 147 analogWrite(sounder_pin, duty_cycle); 148 delay(on_time*100); 149 analogWrite(sounder_pin,0); 150 delay(off_time*100); 151 } 152 } 153 154 // close the file: 155 myFile.close(); 156 } else { 157 // if the file didn't open, print an error: 158 screen_number=file_wont_open_screen; 159 update_display(); 160 } 161 162}//end beep out file 163 164} 165 166}/*end of loop*/ 167 168//my_functions 169 170//my_isr_functions 171void enter_button(void)//enter button 172{ switch(screen_number){case file_menu_screen:beep_file_contents=true;break; 173 case file_wont_open_screen:screen_number=file_menu_screen;do_update=true;break;} 174} 175 176void increment_down()//down button 177{if (screen_number==file_menu_screen){count_updated=true;}} 178 179//my_screen_functions 180void update_display(void) 181{ 182 switch (screen_number) 183 { 184 case start_screen:lcd.clear(); 185 lcd.setCursor(3,0); 186 lcd.print("GymGeneral"); 187 lcd.setCursor(3,1); 188 lcd.print("Gym timer"); 189 break; 190 case initialization_screen:lcd.clear(); 191 lcd.setCursor(0,0); 192 lcd.print("Init SD..."); 193 if (!initialization_fail){lcd.print("done.");}else{lcd.print("fail!");} 194 break; 195 case file_menu_screen:lcd.clear(); 196 lcd.setCursor(2,0); 197 lcd.print("Select file"); 198 lcd.setCursor(2,1); 199 lcd.print(" ");//clear any previous filename (8.3 character format, so 12 spaces) 200 lcd.setCursor(2,1); 201 lcd.print(my_filename); 202 break; 203 case file_wont_open_screen:lcd.clear(); 204 lcd.print("Can't open file"); 205 break; 206 } 207 } 208
Downloadable files
Diagram of connections
Diagram of connections
Comments
Only logged in users can leave comments
glennedi
0 Followers
•0 Projects
Table of contents
Intro
4
0