Components and supplies
Arduino UNO
LCD shield with analog keypad
Deek Robot data logging shield v1.0
Apps and platforms
Arduino IDE
Project description
Code
RPE logger upgrade code file
c_cpp
1//RPE logger code file 2 3// Updated December 2021 - Remove bug which gave extra writes when on rpe 20 and pressed increment and on rpe 6 when pressed decrement 4// "Somehwat hard" altered to "Somewhat hard" 5 6//CHECK IF LCD SHIELD USES DIGITAL PIN 10 AS THIS IS USED AS CS FOR DATA LOGGING SHIELD 7//CHECK TO SEE IF LCD IS ONE OF THE FAULTY ONES 8//REFERENCE - https://forum.arduino.cc/index.php?topic=96747.0 (Retrieved 7/June/2019) 9 10/* 11 The circuit: 12 * SD card attached to SPI bus as follows: 13 ** MOSI - pin 11 14 ** MISO - pin 12 15 ** CLK - pin 13 16 ** CS - pin 10 for deek robot data logging shield v1.0 17 * 18 * RTC connections (I2C): 19 * gnd to gnd 20 * + to +5V 21 * SDA to A4 22 * SCL to A5 23 * 24 * LCD shield: 25 * Backlight - pin 10 removed 26 * B4 to D4 27 * B5 to D5 28 * B6 to D6 29 * B7 to D7 30 * RS to D8 31 * E to D9 32 * 33 * LCD shield buttons: 34 * Analog 0 35 * 36 */ 37 38//#define my_test //uncomment this line to send data to serial console instead of SD card 39 40#include <SPI.h> 41#include <SD.h> 42 43const int chipSelect = 10;//CS for deek robot data logging shield v1.0 44 45//for real time clock 46#include "RTClib.h" 47 48RTC_DS3231 rtc; 49 50//for LCD shield 51#include <LiquidCrystal.h> 52 53// select the pins used on the LCD panel 54LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 55// these have been checked with http://www.robotshop.com/content/PDF/dfrobot-lcd-keypad-shield-schematic.pdf 56 57 58File myFile; 59 60//required to debounce switches 61#define MAX_CHECKS 10 62volatile uint8_t Debounced_State=0;//accessed by isr and main loop code 63uint8_t State[MAX_CHECKS]={0}; 64uint8_t Index=0; 65 66 67//rpe 68const int rpe_max=20; 69const int rpe_min=6; 70 71 72//where we hold the filename 73const String my_filename="data.txt"; 74 75void setup() 76{ 77 78 lcd.begin(16, 2); // start the library 79 lcd.setCursor(0,0); 80 81 pinMode(A0,INPUT);//Analog ladder for buttons 82 83 // initialize timer1 84 noInterrupts(); // disable all interrupts 85 TCCR1A = 0; 86 TCCR1B = 0; 87 TCNT1 = 0; 88 89 OCR1A = 625; // compare match register 16MHX/256/100HZ 90 TCCR1B |= (1 << WGM12); // CTC mode 91 TCCR1B |= (1 << CS12); // 256 prescaler 92 TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt 93 interrupts(); 94 95 96 if (!SD.begin(chipSelect)) { 97 lcd.print("Not Initialized"); 98 delay(5000); 99 return; 100 } 101 102 103 if (!SD.exists(my_filename)) { 104 lcd.print("No File");; 105 delay(5000); 106 } 107 108 lcd.setCursor(4,0); 109 lcd.print("Stopped"); 110 lcd.setCursor(4,1); 111 lcd.print("RPE "); 112 lcd.print(rpe_min); 113 114#ifdef my_test 115 Serial.begin(9600); 116 #endif 117 118} 119 120 121void loop() 122{ 123//variables 124static bool started=false; 125static bool last_entry=false; 126static bool do_update=false; 127static bool button_up=false; 128static bool button_stored=false; 129static int Stored_State=0; 130static int Decode_Me=0; 131static int rpe_count=rpe_min; 132 133 noInterrupts(); // disable all interrupts 134if (Debounced_State>Stored_State){Stored_State=Debounced_State;button_stored=true;}//store and flag as ready 135if (Debounced_State<Stored_State){if(button_stored){button_up=true;Decode_Me=Stored_State;}Stored_State=Debounced_State;}//button has been released 136 interrupts(); // enable all interrupts 137 138if(button_up && button_stored) 139{ 140button_stored=false; 141button_up=false; 142do_update=true; 143 144 if (Decode_Me==1){if(started){rpe_count--;if(rpe_count<rpe_min){rpe_count=rpe_min;do_update=false;}else{do_update=true;} }}//decrement rpe 145 if (Decode_Me==2){if(started){rpe_count++;if(rpe_count>rpe_max){rpe_count=rpe_max;do_update=false;}else{do_update=true;} }}//increment rpe 146 if (Decode_Me==4){if(started){started=false;last_entry=true;}else{started=true;}rpe_count=rpe_min;do_update=true;}//start stop 147 148} 149 150 if (do_update){ 151 do_update=false; 152 153 if(!started){lcd.setCursor(0,0);lcd.print (" Stopped ");} 154 if(rpe_count==6 && started){lcd.setCursor(0,0);lcd.print (" No exertion ");} 155 if(rpe_count>=7 && rpe_count < 9){lcd.setCursor(0,0);lcd.print (" Extremely light ");} 156 if(rpe_count>=9 && rpe_count < 11){lcd.setCursor(0,0);lcd.print (" Very light ");} 157 if(rpe_count>=11 && rpe_count < 13){lcd.setCursor(0,0);lcd.print(" Light ");} 158 if(rpe_count>=13 && rpe_count < 15){lcd.setCursor(0,0);lcd.print(" Somewhat hard ");} 159 if(rpe_count>=15 && rpe_count < 17){lcd.setCursor(0,0);lcd.print(" Hard ");} 160 if(rpe_count>=17 && rpe_count < 19){lcd.setCursor(0,0);lcd.print(" Very hard ");} 161 if(rpe_count>=19 && rpe_count < 20){lcd.setCursor(0,0);lcd.print(" Extremely hard ");} 162 if(rpe_count==20){lcd.setCursor(0,0);lcd.print("Maximum exertion");} 163 164 lcd.setCursor(8,1); 165 lcd.print(rpe_count); 166 if(rpe_count<10){lcd.print(" ");} 167 168//In test mode we write to the serial console when not in test mode we write to the SD card. 169#ifdef my_test 170 if(started){ 171 Serial.print(get_time()); 172 Serial.print('-'); 173 Serial.println(rpe_count);} 174 else 175 {if(last_entry){ 176 last_entry=false; 177 Serial.print(get_time()); 178 Serial.print('-'); 179 Serial.println(0);}} 180 #else 181 if(started){append_file(my_filename,get_time(),rpe_count);}else{if(last_entry){last_entry=false;append_file(my_filename,get_time(),0);}} 182 #endif 183 184 185 } 186 187} 188 189//my_functions 190 191ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 192{ 193// 194//read buttons 195 uint8_t temp=0x00; 196 197 198 if (analogRead(0) > 60 && analogRead(0) < 200){temp|=1<<1;} //btnUP 199 if (analogRead(0) > 200 && analogRead(0) < 400){temp|=1<<0;} //btnDOWN 200 if (analogRead(0) > 600 && analogRead(0) < 800){temp|=1<<2;} //btnSELECT 201 202 203//debounce 204 uint8_t i,j; 205 206//for right hand 207 State[Index]= temp; 208 ++Index; 209 j=0xFF; 210 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 211 Debounced_State=j; 212 if(Index>=MAX_CHECKS){Index=0;} 213 214} 215 216 217//function to get time from clock and return it as a string 218String get_time(void){ 219//check rtc available 220 if (! rtc.begin()) { 221 lcd.print("No RTC"); 222 delay(5000); 223 } 224 225 String dataString = ""; 226 227 DateTime now = rtc.now(); 228 229 dataString += String(now.year()); 230 dataString += ","; 231 dataString += String(now.month()); 232 dataString += ","; 233 dataString += String(now.day()); 234 dataString += ","; 235 dataString += String(now.hour()); 236 dataString += ","; 237 dataString += String(now.minute()); 238 dataString += ","; 239 dataString += String(now.second()); 240 241 return dataString; 242 } 243 244//function to append the rpe value and time to the data file 245void append_file(String filename,String dataString,int rpe) 246{ 247 248//append rate perceived exertion value to time dataString 249 dataString += ','; 250 dataString += rpe; 251 dataString += ','; 252 253 254//write to file 255 256 myFile = SD.open(filename, FILE_WRITE); 257 258 // if the file opened okay, write to it: 259 if (myFile) { 260 myFile.println(dataString); 261 // close the file: 262 myFile.close(); 263 } else { 264 lcd.print("Can't open file");//error file didn't open when writing data 265 delay(5000); 266 } 267 268return; 269} 270//End 271
Comments
Only logged in users can leave comments
glennedi
0 Followers
•0 Projects
Table of contents
Intro
4
0