Help needed with a program

Good afternoon all

i wounder if someone can help me I have a project to flash a surround on a push switch and once pressed display thank you on a 16x2 display then return to the welcome message

i have verified the program but it will not work if i just run with the switch program on its on all is ok and the same with the display but put them both together is will not work
Anyone available to tell me what I'm doing wrong

`// This is a program designed to flash the switch surround at 3hz and then once the push button 
// is pressed will write thank you on the display then return to the welcome message
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27, 16, 2); // Please set the LCD address to 0x27 as instructed from the i2c scanner feedback 

 
#define ledOn LOW
#define ledOff HIGH
const int ledPin =  12;
const int buttonPin = 6;   
int ledState = ledOff;                         // the current state of the LED
int previousState = LOW;                       // the previous buttonState from the button pin
unsigned long buttonTime = 3;                  // The last time the output pin was toggled
unsigned long debounceTime = 600;              // debounceTime time (For button)


void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);


 lcd.init();         // initialize the lcd
 lcd.backlight();    // Turn on the LCD screen backlight

}


void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState != previousState && millis() - buttonTime > debounceTime) {
    ledState = !ledState;
    buttonTime = millis();
  }

  if (ledState == ledOn) {
    digitalWrite(ledPin, (millis() >> 6) & 4);  //Blink
  } else
    digitalWrite(ledPin, ledOff);

  previousState = buttonState;
  






 lcd.setCursor(2, 0);
  lcd.print("WELCOME USER");
  delay(3000),
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("  Please press  ");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("The Start Button   ");
  delay(1000),
  lcd.setCursor(0, 1);
  lcd.print("      Below     ");
  delay(1000);
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("To Begin ");
  delay(1000);
  lcd.clear();
}
`

Please post your sketch, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

Thank you for the advice sorry only a beginner on here

Thank you for adding the code tags. I hope that you can see how much easier it is to read the code when you use them


// This is a program designed to flash the switch surround at 3hz and then once the push button 
// is pressed will write thank you on the display then return to the welcome message
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27, 16, 2); // Please set the LCD address to 0x27 as instructed from the i2c scanner feedback 

 
#define ledOn LOW
#define ledOff HIGH
const int ledPin =  12;
const int buttonPin = 10;   
int ledState = ledOff;                         // the current state of the LED
int previousState = LOW;                       // the previous buttonState from the button pin
unsigned long buttonTime = 3;                  // The last time the output pin was toggled
unsigned long debounceTime = 600;              // debounceTime time (For button)


void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);


lcd.init();         // initialize the lcd
lcd.backlight();    // Turn on the LCD screen backlight



}
void loop() {
      lcd.setCursor(1, 0);
      lcd.print("simon smith  "); 
      delay(500),

      lcd.setCursor(5,1);
      lcd.print("project");        
      delay(3000),
      lcd.clear();

      lcd.setCursor(2, 0);
      lcd.print("WELCOME USER");
      delay(3000),
      lcd.clear();

      lcd.setCursor(0, 1);
      lcd.print("I'm bot v1");
      delay(2000);
      lcd.setCursor(0, 1);
      lcd.print("  Please press  ");
      delay(1000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("The Start Button   ");
      delay(1000),
      lcd.setCursor(0, 1);
      lcd.print("      Below     ");
      delay(1000);
      lcd.clear();
      lcd.setCursor(4, 0);
      lcd.print("To Begin ");
      delay(1000);
      lcd.clear();
     
  
   int buttonState = digitalRead(buttonPin);
   if (buttonState != previousState && millis() - buttonTime > debounceTime) 
   ledState = !ledState;
   buttonTime = millis();
   if (ledState == ledOn) 
   digitalWrite(ledPin, (millis() >> 6) & 4);  //Blink
  
  else
  digitalWrite(ledPin, ledOff);    
  previousState = buttonState;
}

What do you mean by this ?

In your sketch you have delay()s totalling 7 seconds that occur for every iteration of the loop. so the button will only be read every 7 seconds

Is that what you intend to happen ?

Hi HeliBob

After playing with the code above thread
I now have the display working but only a static switch ring
If i split the program down the ring on the switch works as the program flashing until pressed on release the ring returns to flashing
but together static ring around the switch for 60 seconds then off for 30 seconds and repeat really makes no sence or am i over thinking it

Hi HeliBob
I thought this was the correct way to delay between screens not thinking im affecting the hole project ?

delay() does what it says, ie it delays the sketch for the period specified. This is known as blocking code

If you want to detect the button input whilst displaying the messages so that the user can press the button at any time and have it recognised then you will need to use non blocking timing

Start by looking at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

1 Like

From wiring.c...

void delay(unsigned long ms)
{
	uint32_t start = micros();

	while (ms > 0) {
		yield();
		while ( ms > 0 && (micros() - start) >= 1000) {
			ms--;
			start += 1000;
		}
	}
}

I think i get the idea can you explain it a little from my code above i think it will help

delay() stops all processes in the microcontroller.

Post #9 describes it as blocking code... because nothing happens during (7 seconds) delay() For your program, that means no button read or LEDs change during 7 seconds, and only the brief time of reading at the beginning of loop to do both.

HeliBob
Thank you again I finally get it makes perfect sense now.
And thank you again for your post on millis it’s really helpful and now makes perfect sense once I understand the time line and how to fix an event every 1000 ms saved me so much time

Kindest regards
Simon

I am glad that it makes sense and I hope that you can move forward with your project

I have been meaning to add some more examples to the tutorial based on questions involving non blocking timing raised in the forum

1 Like

I can now Thank you I was really not thinking logically but makes perfect sense now

I’m new to C and C+ and the Arduino
My background of the last 10 years is in automation of machines and robotics on the industrial scale

But thanks again for your help I think a new post would help others too

One of the problems of providing examples is that users have often gotten a long way into writing their sketch using delay() before they realise that there is a problem. Non blocking timing using millis() requires a completely different approach to the structure of the sketch as you can't just replace delay() with a millis() solution

It is rather like the story of the motorist who asked someone how to get to somewhere only to be told that if they wanted to get there by the best route then they would not be starting from here

1 Like

It's worse:

A person asks for directions to a particular place. A local struggles to describe a series of convoluted directions and soon concludes, "You know, come to think of it, you can't get there from here!"

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.