Hi,
I'm really new to coding, so I really have no idea how to code or understand the terms. I have an assignment that needs to have a button that starts/stops and resets like a stopwatch timer using millis. I've heard of debouncing but cant seem to wrap my head around the coding aspect of it. If someone can help me with it, even a link that shows this. Would be 100% awesome..
This is my code that I have so far
#include <U8glib.h>
U8GLIB_SSD1351_128X128_332 u8g(13, 11, 8, 9, 7); // Arduino UNO: SW SPI Com: SCK = 13, MOSI = 11, CS = 8, DC = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
int Heart = A0;
int value = 0;
int Motor = 12;
int motorRead = 6;
int Thresh = 350;
int ReadRate = 25;
int UnderCount = 0;
int OverCount = 0;
String OutValue = "";
char messageBuffer[50] = {0};
void setup() {
// flip screen, if required
// u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
// put your setup code here, to run once:
Serial.begin(19200);
pinMode(Motor, OUTPUT);
//delay(5000);
}
void loop() {
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
// put your main code here, to run repeatedly:
value = analogRead(Heart);
Serial.println(value);
if (value > 550){
OverCount = OverCount + 1;
UnderCount = UnderCount + 1;
}
else{
UnderCount = UnderCount + 1;
}
float TestF = (OverCount/(UnderCount*0.1)*100);
String OutValue = String (TestF/2);
//Serial.println(OutValue + " BPM");
Serial.println(value);
if (value > Thresh ){
digitalWrite(Motor, HIGH);
digitalWrite(motorRead, HIGH);
delay(50);
digitalWrite(Motor, LOW);
digitalWrite(motorRead, LOW);
}
else{
delay(50);
}
if (UnderCount > 50){
UnderCount = 0;
OverCount = 0;
}
delay(ReadRate);
stopWatch();
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
//int value = 0;
//char buf[9];
//sprintf (buf, "%d", value);
//u8g.drawStr( 0, 40, buf);
String value = "test";
value = analogRead(Heart);
u8g.setPrintPos( 0, 40 );
u8g.print(messageBuffer);
u8g.setPrintPos( 0, 80);
u8g.print(value);
u8g.print(" BPM");
}
void stopWatch(void) {
memset(messageBuffer, '\0', sizeof(messageBuffer));
static unsigned long current_time;
unsigned long remainder, minutes, seconds, milliseconds;
current_time = millis();
milliseconds = current_time % 1000;
remainder = current_time / 1000;
seconds = remainder % 60;
remainder /= 60;
minutes = remainder % 60;
addToMessageBuffer(minutes, 2, true);
addToMessageBuffer(seconds, 2, true);
addToMessageBuffer(milliseconds, 3, false);
Serial.println(messageBuffer);
}
void addToMessageBuffer(unsigned long digit, int len, bool appendColon) {
char str[10] = {0};
//Convert digit to string
sprintf(str, "%lu", digit);
//Pad with as many zeros as desired
while (strlen(str) < len) {
char temp[10] = {0};
strcat(temp, "0");
strcat(temp, str);
strcpy(str, temp);
}
//Add to messageBuffer
strcat(messageBuffer, str);
//Append colon if desired
if (appendColon == true) {
strcat(messageBuffer, ":");
}
}