Hi!
I'm writing a code for my project and I noticed some really weird stuff! Basically if I want to use delay function in my code for some reason I need to write the value which is 100 bigger to get the desired delay! So if I want five seconds delay then I need to write delay(500 000) and not delay(5000), even after this I get 8,20s delay???
My guess is that this happens because I use hardware interrupts in my code but how can I use interrupts and delay at the same time?
Here is my code:
#include <SPI.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
// For the breakout, you can use any (4 or) 5 pins
#define sclk 52 //13 //52
#define mosi 51//11 //51
#define cs 53//10 //53
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset
// Option 1: use any pins but a little slower
//Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
// Counts number of button presses
// output count to serial
// blink a led according to count
#define BUTTON1_PIN 2 //seöect switch
const unsigned int DEBOUNCE_TIME = 200;
const unsigned int REFRESH_TIME = 300;
byte buttonInterrupt = 0;
unsigned int counter = 0;
static unsigned long last_interrupt_time = 0;
byte switchPin1 = 18; // switch 1 +
byte switchPin2 = 19; // switch 2 -
byte buttonPresses = 0; // how many times the button has been pressed
byte lastPressCount = 0; // to keep track of last press count
byte mainmenu=0;
byte phmenucount=0;
byte phcal = 0;
void setup() { //*************************************SETUP*****************************************************************
Serial.begin(9600); // Set up serial communication at 9600bps
pinMode(BUTTON1_PIN, INPUT);
digitalWrite(BUTTON1_PIN, HIGH); // Set the switch pin as input
attachInterrupt(0, button1, FALLING); //interrupt
pinMode(switchPin1, INPUT); // Set the switch pin as input
digitalWrite(switchPin1, HIGH); // set pullup resistor
pinMode(switchPin2, INPUT); // Set the switch pin as input
digitalWrite(switchPin2, HIGH); // set pullup resistor
tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
Serial.println("init");
}
void loop(){ //*************************************LOOP*****************************************************************
switch (mainmenu) {
case 1:
if (digitalRead(switchPin1) == LOW) // check if button was pressed
{
buttonPresses++; // increment buttonPresses count
delay(500); // debounce switch
}
if (digitalRead(switchPin2) == LOW) // check if button was pressed
{
buttonPresses--; // increment buttonPresses count
delay(500); // debounce switch
}
if (buttonPresses == 6) buttonPresses = 1;
if (buttonPresses == 0) buttonPresses = 5; // rollover every fourth press
if (lastPressCount != buttonPresses) // only do output if the count has changed
{
Serial.print ("Main menu count: "); // out to serial
Serial.println(buttonPresses, DEC);
lastPressCount = buttonPresses; // track last press count
}
break;
}
}
void phcalwindow(){
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_GREEN);
tft.setCursor(3, 2);
tft.setTextSize(2);
tft.print("pH ");
tft.setCursor(30, 10);
tft.setTextSize(1);
tft.print(" Calibration");
delay (500000); //<<<<<<<<< this is the delay!!!!!!!!!!!!
tft.print(" Done!");
}
void eccalwindow(){
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_GREEN);
tft.setCursor(1, 140);
tft.print("EC Calibration");
}
void otherwindow(){
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_GREEN);
tft.setCursor(1, 140);
tft.print("Other stuff");
}
void infowindow(){
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_GREEN);
tft.setCursor(1, 140);
tft.print("Info");
}
void button1(){ //*************************************BUTTON1*****************************************************************
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > DEBOUNCE_TIME) {
Serial.println("Button 1 (Select)");
if(buttonPresses == 0) {
Serial.println ("Show menu window");
buttonPresses = 6;
mainmenu = 1;
}
if(buttonPresses == 1){
Serial.println ("pH cal start");
phcalwindow();
mainmenu = 0;
}
if(buttonPresses == 2){
Serial.println ("EC cal start");
mainmenu = 0;
}
if(buttonPresses == 4){
Serial.println ("Other");
mainmenu = 0;
}
if(buttonPresses == 5){
Serial.println ("Exit to main window");
delay(100);
mainmenu = 0;
buttonPresses = 0;
}
}
last_interrupt_time = interrupt_time;
}
Any help would be much appreciated!