Hi
This is my first post on the forums. I do try to find the information out myself and learn as I go but I'm pretty much stuck at the moment.
I'm very new to coding for the Arduino but do a lot of php programming and I think I'm in php mode when trying to do if else statements, while loops, switch's etc and not quite sure if what im doing is correct.
Let me start by explaining what I want to do. I have built tried and tested an ac light flicker circuit. that works great. I have programmed 3 different modes that I want to be able to access with the push of a button. I left the coding for the flicker circuit out for now and concentrated on making 3 different modes that output their program names to an LCD. that all works fine as expected. I press a button and it will scroll through the different programs.
now this is where I get stuck, I have tried various different statements, if, where etc to loop through a very basic sequence. in this case I tried it with a blinking led with 3 different speed modes. First I was using delays and could not break out of a while loop. I did some more research and changed to milli and still could not break out. Looking at my code below what is the best possible way for me to 'load in' my different programs? I would like to be able to use this on other projects so its not specific to my blinking led's or the actual; flicker circuit code but pretty much for anything.
so heres my code so far :
// include the library code:
#include <LiquidCrystal.h>
// LCD to pin number
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const byte buttonPin = 7; // pin button is connected
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = LOW; // current state of the button
boolean lastButtonState = LOW; // previous state of the button
int led = 8; // pin LED is connected to
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX"); // load up screen text line 1
lcd.setCursor(0, 1);
lcd.print("LIGHT FLICKER V1"); // load up screen text line 2
pinMode(buttonPin, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the pushbutton input pin:
if (buttonState != lastButtonState) // compare the buttonState to its previous state
{
if (buttonState == HIGH)
{
buttonPushCounter++; // add one to counter
lcd.clear();
if (buttonPushCounter > 3) // if counter is over 3 reset the counter to 1 to show Program 1 and not load up text
{
buttonPushCounter = 1;
}
Serial.println(buttonPushCounter);
if (buttonPushCounter == 1){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P1 - FLICKER");
}
else if (buttonPushCounter == 2){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P2 - FLICKER");
}
else if (buttonPushCounter == 3){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P3 - FLICKER");
}
else {
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("NO PROGRAM");
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
}}
now what is the best way for me to insert my code like below :
if (buttonPushCounter == 1){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P1 - FLICKER");
// INSERT CODE HERE //
}
else if (buttonPushCounter == 2){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P2 - FLICKER");
//INSERT CODE HERE //
}
else if (buttonPushCounter == 3){
lcd.setCursor(0, 0);
lcd.print("4WARD THEME WORX");
lcd.setCursor(0, 1);
lcd.print("P3 - FLICKER");
//INSERT CODE HERE //
}
And so on but be able to break out of each program and move on to the next each time the button is pressed?
Any ideas, guidance or help would be very much appreciated.