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.
// 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;
}
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
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
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
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
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!"