Inspection & advice required on my count down timer program.

Hi

i have made a program for count down timer.Whenever event occurred O/P signal become HIGH and count down started from the preset time.After time reach the zero, O/P signal become LOW.

I have tested this working fine.But i am not confident on which i have done.kindly advice me if i have done any mistakes on the program.

This is my code:-

#include <Adafruit_GFX.h>////////////////oled
#include <Adafruit_SSD1306.h>////////////oled
#define OLED_Address 0x3C///////////////oled

Adafruit_SSD1306 oled(1);///////////////oled

#include <Fonts/FreeSerifItalic9pt7b.h>///OLED
#include <Fonts/FreeMono9pt7b.h>////oled


#define SCREEN_WIDTH 128 // /////////////////////////////////OLED display width, in pixels
#define SCREEN_HEIGHT 64 // /////////////////////////////////OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);//OLED display height, in pixels




int LEDPin=13;
int buttonPin=2;
int buttonNew;
int buttonOld=1;
int dt=100;
int i=10;
 
void setup() {
  
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(25, 0);
  oled.print("WELCOME");
  oled.display();
  delay(6000);
}

void loop() {

buttonNew=digitalRead(buttonPin);
if(buttonOld==0 && buttonNew==1){
  digitalWrite(LEDPin,HIGH);
  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(WHITE);
  oled.setCursor(25,0);
  oled.print("PLEASE WAIT....");
  oled.drawLine(0, 12, 127, 12, WHITE);
  oled.display();
  while (i>0)
  {
 
      i--;
      
      Serial.println(i);
      oled.setTextSize(2);
      oled.setTextColor(WHITE,BLACK);
      oled.setCursor(55, 17);
      oled.print(i);
      delay(1000);
      oled.display(); 
   
  }
   
   digitalWrite(LEDPin,LOW);
}
else{
   oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(WHITE);
  oled.setCursor(25, 0);
  oled.print("READY TO START");
  oled.drawLine(0, 12, 127, 12, WHITE);
  oled.setTextSize(2);
  oled.setCursor(55, 17);
  oled.print(i);
  oled.display();
  i=10;
  }
buttonOld=buttonNew;
delay(dt);
}

Quite a short program then :slight_smile:

UKHeliBob:
Quite a short program then :slight_smile:

sorry..code added

[sarcasm]Thank you for making my reply nonsense by adding the code to your original post after I had commented on it[/sarcasm]

(deleted)

If the program works as intended then you have met your primary objective but it is always good to think ahead because requirements can, and do, change

For instance, how easy would it be to change your program such that pressing a pushbutton attached to an input pin caused the countdown to stop and immediately start counting down again from the beginning ?

As to the current program, how many times does it print the "READY TO START" message to the screen when waiting for user input and is the button input wired so that it is always in a known state (HIGH or LOW) and not floating at an unknown voltage ? If you have a pulldown resistor in place, which I doubt, can you change the program to eliminate the need for it ? Why are you using 0 and 1 as the input states when LOW and HIGH would be more conventional ?