Components and supplies
Prototype shield
LCD shield
Arduino UNO
Piezo sounder
Apps and platforms
Arduino IDE
Project description
Code
Fintess timer code
c_cpp
1 2//simple fitness timer 3// 4 5/* 6 The circuit: 7 8 * 1* sounder pin 3 9 10 * LCD shield: 11 * Backlight - pin 10 removed 12 * B4 to D4 13 * B5 to D5 14 * B6 to D6 15 * B7 to D7 16 * RS to D8 17 * E to D9 18 * 19 * LCD shield buttons: 20 * Analog 0 21*/ 22 23//CHECK TO SEE IF LCD IS ONE OF THE FAULTY ONES 24//MINE HAD PROBLEMS SO I DESOLDERED AND REMOVED THE FAULTY PIN 10 25//REFERENCE - https://forum.arduino.cc/index.php?topic=96747.0 (Retrieved 7/June/2019) 26//for the LCD shield 27//my_aliases 28 29//for the LCD shield 30#include <LiquidCrystal.h> 31 32// select the pins used on the LCD panel 33LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 34// http://www.robotshop.com/content/PDF/dfrobot-lcd-keypad-shield-schematic.pdf 35 36//required to debounce switch 37#define MAX_CHECKS 10 38volatile uint8_t Debounced_State=0;//accessed by isr and main loop code 39uint8_t State[MAX_CHECKS]={0}; 40uint8_t Index=0; 41 42//for the buzzer 43const int sounder_pin=3;// the PWM pin the buzzer is attached to 44int duty_cycle = 127; // 0-255 so 50% = 127 approx 45int hi_frequency=0x02; 46const int low_frequency=0x03; 47 48//seconds for work and rest periods and set toggle to false 49const long int work_array[]={20,30,60,90}; 50const int work_array_max=3; 51const long int rest_array[]={20,30,60,90}; 52const int rest_array_max=3; 53bool toggle_on=false; 54 55void setup() { 56 57//initial lcd setup 58 lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight 59 lcd.clear(); 60 61 update_display(work_array[0],rest_array[0],false); 62 delay(5000); 63 64 // initialize digital pin as an output. 65 pinMode(sounder_pin, OUTPUT); 66 67 // initialize timer1 68 noInterrupts(); // disable all interrupts 69 TCCR1A = 0; 70 TCCR1B = 0; 71 TCNT1 = 0; 72 73 OCR1A = 625; // compare match register 16MHX/256/100HZ 74 TCCR1B |= (1 << WGM12); // CTC mode 75 TCCR1B |= (1 << CS12); // 256 prescaler 76 TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt 77 interrupts(); 78 79 80}/*end of setup*/ 81 82void loop() { 83 84//variables 85bool count_updated=true; 86bool toggle_pressed=false; 87bool work_updated=false; 88bool rest_updated=false; 89bool start_work=false; 90bool start_rest=false; 91 92static bool button_up=false; 93static bool chord_ready=false; 94static int Stored_State=0; 95static int Action_Me=0; 96 97static bool enter_pressed=false; 98 99static int work_level=0; 100static int rest_level=0; 101 102static int work_count=0; 103static int rest_count=0; 104 105static bool work_done=false; 106static bool rest_done=false; 107 108while(1){ 109 110 noInterrupts(); // disable all interrupts 111if (Debounced_State>Stored_State){Stored_State=Debounced_State;chord_ready=true;}//store chord and flag as ready 112if (Debounced_State<Stored_State){if(chord_ready){button_up=true;Action_Me=Stored_State;}Stored_State=Debounced_State;} 113 interrupts(); // enable all interrupts 114 115if(button_up && chord_ready) 116{ 117chord_ready=false; 118button_up=false; 119 120if (Action_Me&1<<0){toggle_pressed=true;}//toggle button 121if (Action_Me&1<<2){work_updated=true;}//work button 122if (Action_Me&1<<3){rest_updated=true;}//rest button 123 124if (Action_Me&1<<1){start_work=true;}//start work button 125if (Action_Me&1<<4){start_rest=true;}//start rest button 126/**/ 127 128} 129 130 if (toggle_pressed){ 131 132 count_updated=false; 133 toggle_pressed=false; 134 toggle_on=!toggle_on; 135 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 136 } 137 138if (work_updated){ 139 140 //enter_pressed=false; 141 work_updated=false; 142 work_level++; 143 if (work_level>work_array_max){work_level=0;} 144 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 145 } 146 147if (rest_updated){ 148 149 //enter_pressed=false; 150 rest_updated=false; 151 rest_level++; 152 if (rest_level>rest_array_max){rest_level=0;} 153 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 154 } 155 156if (start_work){ 157 158 TCCR2B = TCCR2B & 0b11111000 | hi_frequency;//PWM frequency 159 160 if(work_count!=work_array[work_level] && !work_done){ 161 analogWrite(sounder_pin, duty_cycle); 162 delay(100); 163 analogWrite(sounder_pin,0); 164 delay(900); 165 work_count++; 166 }else{work_count=0;if(toggle_on){start_rest=true;}else{analogWrite(sounder_pin, duty_cycle); 167 delay(300);analogWrite(sounder_pin,0);};start_work=false;} 168 } 169 170 171if (start_rest){ 172 173 TCCR2B = TCCR2B & 0b11111000 | low_frequency;//PWM frequency 174 175 if(rest_count!=rest_array[rest_level]){ 176 analogWrite(sounder_pin, duty_cycle);//pin,frequency,duration in milliseconds 177 delay(100); 178 analogWrite(sounder_pin,0); 179 delay(900); 180 rest_count++; 181 }else{rest_count=0;if(toggle_on){start_work=true;}else{analogWrite(sounder_pin, duty_cycle);//pin,frequency,duration in milliseconds 182 delay(300);analogWrite(sounder_pin,0);};start_rest=false;} 183 } 184/**/ 185 186}//end of program 187 188 189 190 191 192 193}/*end of loop*/ 194 195//my_functions 196 197ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 198{ 199 200//read buttons 201 uint8_t temp=0x00; 202/* 203 * //rough values from read of analog buttons 204636 //select - togggle 205408 //left - start work 206254 //donwn - increment work 207101 //up - increment rest 2080 //right - start rest 209*/ 210 if (analogRead(0) > 600 && analogRead(0) < 650){temp|=1<<0;} //btnTOGGLE 211 if (analogRead(0) > 380 && analogRead(0) < 450){temp|=1<<1;} //btnSTART_WORK 212 if (analogRead(0) > 200 && analogRead(0) < 300){temp|=1<<2;} //btnUP_WORK 213 if (analogRead(0) > 50 && analogRead(0) < 150){temp|=1<<3;} //btnUP_REST 214 if (analogRead(0) >= 0 && analogRead(0) < 45){temp|=1<<4;} //btnSTART_REST 215 216//debounce 217 uint8_t i,j; 218 State[Index]= temp; 219 ++Index; 220 j=0xFF; 221 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 222 Debounced_State=j; 223 if(Index>=MAX_CHECKS){Index=0;} 224} 225 226 227//my_screen_functions 228void update_display(long int work_period, long int rest_period, bool toggle_on) 229{ 230 lcd.clear(); 231 lcd.setCursor(0,0); 232 lcd.print("Work Rest Togg"); 233 lcd.setCursor(1,1); 234 lcd.print(work_period); 235 lcd.setCursor(7,1); 236 lcd.print(rest_period); 237 lcd.setCursor(11,1); 238 if (toggle_on){lcd.print("True");}else{lcd.print("False");} 239 }//end of update screen 240
Fintess timer code
c_cpp
1 2//simple fitness timer 3// 4 5/* 6 The circuit: 7 8 * 9 1* sounder pin 3 10 11 * LCD shield: 12 * Backlight - pin 10 removed 13 * B4 14 to D4 15 * B5 to D5 16 * B6 to D6 17 * B7 to D7 18 * RS to D8 19 * E to D9 20 21 * 22 * LCD shield buttons: 23 * Analog 0 24*/ 25 26//CHECK TO SEE IF LCD IS 27 ONE OF THE FAULTY ONES 28//MINE HAD PROBLEMS SO I DESOLDERED AND REMOVED THE FAULTY 29 PIN 10 30//REFERENCE - https://forum.arduino.cc/index.php?topic=96747.0 (Retrieved 31 7/June/2019) 32//for the LCD shield 33//my_aliases 34 35//for the LCD shield 36#include 37 <LiquidCrystal.h> 38 39// select the pins used on the LCD panel 40LiquidCrystal 41 lcd(8, 9, 4, 5, 6, 7); 42// http://www.robotshop.com/content/PDF/dfrobot-lcd-keypad-shield-schematic.pdf 43 44 45//required to debounce switch 46#define MAX_CHECKS 10 47volatile uint8_t 48 Debounced_State=0;//accessed by isr and main loop code 49uint8_t State[MAX_CHECKS]={0}; 50uint8_t 51 Index=0; 52 53//for the buzzer 54const int sounder_pin=3;// the PWM pin the 55 buzzer is attached to 56int duty_cycle = 127; // 0-255 so 50% = 127 approx 57int 58 hi_frequency=0x02; 59const int low_frequency=0x03; 60 61//seconds for work and 62 rest periods and set toggle to false 63const long int work_array[]={20,30,60,90}; 64const 65 int work_array_max=3; 66const long int rest_array[]={20,30,60,90}; 67const int 68 rest_array_max=3; 69bool toggle_on=false; 70 71void setup() { 72 73//initial 74 lcd setup 75 lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn 76 on backlight 77 lcd.clear(); 78 79 update_display(work_array[0],rest_array[0],false); 80 81 delay(5000); 82 83 // initialize digital pin as an output. 84 pinMode(sounder_pin, 85 OUTPUT); 86 87 // initialize timer1 88 noInterrupts(); // disable 89 all interrupts 90 TCCR1A = 0; 91 TCCR1B = 0; 92 TCNT1 = 0; 93 94 OCR1A 95 = 625; // compare match register 16MHX/256/100HZ 96 TCCR1B |= (1 97 << WGM12); // CTC mode 98 TCCR1B |= (1 << CS12); // 256 prescaler 99 TIMSK1 100 |= (1 << OCIE1A); // enable timer compare interrupt 101 interrupts(); 102 103 104}/*end 105 of setup*/ 106 107void loop() { 108 109//variables 110bool count_updated=true; 111bool 112 toggle_pressed=false; 113bool work_updated=false; 114bool rest_updated=false; 115bool 116 start_work=false; 117bool start_rest=false; 118 119static bool button_up=false; 120static 121 bool chord_ready=false; 122static int Stored_State=0; 123static int Action_Me=0; 124 125static 126 bool enter_pressed=false; 127 128static int work_level=0; 129static int rest_level=0; 130 131static 132 int work_count=0; 133static int rest_count=0; 134 135static bool work_done=false; 136static 137 bool rest_done=false; 138 139while(1){ 140 141 noInterrupts(); // 142 disable all interrupts 143if (Debounced_State>Stored_State){Stored_State=Debounced_State;chord_ready=true;}//store 144 chord and flag as ready 145if (Debounced_State<Stored_State){if(chord_ready){button_up=true;Action_Me=Stored_State;}Stored_State=Debounced_State;} 146 147 interrupts(); // enable all interrupts 148 149if(button_up && 150 chord_ready) 151{ 152chord_ready=false; 153button_up=false; 154 155if (Action_Me&1<<0){toggle_pressed=true;}//toggle 156 button 157if (Action_Me&1<<2){work_updated=true;}//work button 158if 159 (Action_Me&1<<3){rest_updated=true;}//rest button 160 161if (Action_Me&1<<1){start_work=true;}//start 162 work button 163if (Action_Me&1<<4){start_rest=true;}//start rest button 164/**/ 165 166} 167 168 169 if (toggle_pressed){ 170 171 count_updated=false; 172 toggle_pressed=false; 173 174 toggle_on=!toggle_on; 175 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 176 177 } 178 179if (work_updated){ 180 181 //enter_pressed=false; 182 183 work_updated=false; 184 work_level++; 185 if (work_level>work_array_max){work_level=0;} 186 187 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 188 } 189 190if 191 (rest_updated){ 192 193 //enter_pressed=false; 194 rest_updated=false; 195 rest_level++; 196 197 if (rest_level>rest_array_max){rest_level=0;} 198 update_display(work_array[work_level],rest_array[rest_level],toggle_on); 199 200 } 201 202if (start_work){ 203 204 TCCR2B = 205 TCCR2B & 0b11111000 | hi_frequency;//PWM frequency 206 207 if(work_count!=work_array[work_level] 208 && !work_done){ 209 analogWrite(sounder_pin, duty_cycle); 210 delay(100); 211 212 analogWrite(sounder_pin,0); 213 delay(900); 214 work_count++; 215 }else{work_count=0;if(toggle_on){start_rest=true;}else{analogWrite(sounder_pin, 216 duty_cycle); 217 delay(300);analogWrite(sounder_pin,0);};start_work=false;} 218 219 } 220 221 222if (start_rest){ 223 224 TCCR2B 225 = TCCR2B & 0b11111000 | low_frequency;//PWM frequency 226 227 if(rest_count!=rest_array[rest_level]){ 228 229 analogWrite(sounder_pin, duty_cycle);//pin,frequency,duration in milliseconds 230 231 delay(100); 232 analogWrite(sounder_pin,0); 233 delay(900); 234 rest_count++; 235 236 }else{rest_count=0;if(toggle_on){start_work=true;}else{analogWrite(sounder_pin, 237 duty_cycle);//pin,frequency,duration in milliseconds 238 delay(300);analogWrite(sounder_pin,0);};start_rest=false;} 239 240 } 241/**/ 242 243}//end of program 244 245 246 247 248 249 250 251}/*end of loop*/ 252 253//my_functions 254 255ISR(TIMER1_COMPA_vect) 256 // timer compare interrupt service routine 257{ 258 259//read buttons 260 261 uint8_t temp=0x00; 262/* 263 * //rough values from read of analog buttons 264636 265 //select - togggle 266408 //left - start work 267254 //donwn - increment work 268101 269 //up - increment rest 2700 //right - start rest 271*/ 272 if (analogRead(0) 273 > 600 && analogRead(0) < 650){temp|=1<<0;} //btnTOGGLE 274 if (analogRead(0) 275 > 380 && analogRead(0) < 450){temp|=1<<1;} //btnSTART_WORK 276 if (analogRead(0) 277 > 200 && analogRead(0) < 300){temp|=1<<2;} //btnUP_WORK 278 if (analogRead(0) > 279 50 && analogRead(0) < 150){temp|=1<<3;} //btnUP_REST 280 if (analogRead(0) >= 0 281 && analogRead(0) < 45){temp|=1<<4;} //btnSTART_REST 282 283//debounce 284 uint8_t 285 i,j; 286 State[Index]= temp; 287 ++Index; 288 j=0xFF; 289 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 290 291 Debounced_State=j; 292 if(Index>=MAX_CHECKS){Index=0;} 293} 294 295 296//my_screen_functions 297void 298 update_display(long int work_period, long int rest_period, bool toggle_on) 299{ 300 301 lcd.clear(); 302 lcd.setCursor(0,0); 303 lcd.print("Work Rest Togg"); 304 305 lcd.setCursor(1,1); 306 lcd.print(work_period); 307 lcd.setCursor(7,1); 308 309 lcd.print(rest_period); 310 lcd.setCursor(11,1); 311 if (toggle_on){lcd.print("True");}else{lcd.print("False");} 312 313 }//end of update screen 314
Downloadable files
Shield piezo top view
You just need the piezo sounder for this project
Shield piezo top view
How everything fits together
How everything fits together
Buzzer connections
Buzzer connections
How everything fits together
How everything fits together
Buzzer connections
Buzzer connections
Shield piezo connections
Shield piezo connections
Shield piezo top view
You just need the piezo sounder for this project
Shield piezo top view
Comments
Only logged in users can leave comments
glennedi
0 Followers
•0 Projects
Table of contents
Intro
3
0