• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Multi Color LED, Need help with loop statement

scarygood536

New Member
Hello there,

I hope this is in the right section, seeing how this is a arduino related question. I have written code that allows a Radioshack multicolored LED to rotate between Blue, Green and Red. While that is in it's loop a LCD display reads out the color. Now I added a Title before the colors blink to tell the person what's going to happen. I can't find a way to just get that to appear once, just at the beginning.

Code:
// Blink Multi Color LED +5 common, LCD
  
  
 #include <LiquidCrystal.h> //LCD library
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // set LCD PINS
int BL = 13; // Backlight
// Pin 13 has an LED connected on most Arduino boards.
int ledC = 8;
int ledR = 9;
int ledB = 7;
int ledG = 6;



// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(ledC, OUTPUT);  
 pinMode(ledG, OUTPUT);
 pinMode(ledB, OUTPUT);
 pinMode(ledR, OUTPUT); 
 pinMode(BL, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() 
{
  lcd.begin(16,2); //sets display to start
  lcd.setCursor(0, 0); //sets cursor 1st row
  lcd.print("Booting...");
  delay(1000); //wait for a second
   digitalWrite(BL,HIGH); //turn backlight on
  digitalWrite(ledC, HIGH);  // multi color + pin
  digitalWrite(ledR, HIGH); // Stops sourcing of current
  digitalWrite(ledG, HIGH); // Stops sourcing of current
  digitalWrite(ledB, HIGH); // Stops sourcing of current
    lcd.setCursor(0, 0); //Cursor set to top line
     lcd.print("MultiColor LED,"); //display text
     lcd.setCursor(0, 1); // Sets cursor to 2 row
     lcd.print("rotating colors");
        delay(5000); // wait in current state for 5 seconds
        

 digitalWrite(ledB, LOW);  // Blue LED lights
     lcd.begin(16, 2);
     lcd.print("Blue");
    delay(5000);               // wait for 5 seconds
  digitalWrite(ledB, HIGH);
  digitalWrite(ledG, LOW); //Green LED lights, Blue off
  lcd.begin(16, 2);
     lcd.print("Green");
   delay(5000); //on for 5 seconds
  digitalWrite(ledG, HIGH);
  digitalWrite(ledR, LOW); //Red LED lights, Green off
   lcd.begin(16, 2);
     lcd.print("Red");
  delay(5000);
  digitalWrite(ledR,HIGH); //Red LED off
       lcd.begin(16, 2);
     lcd.clear(); //Clears LCD display
  
}
I would like the "booting" and "Multicolor led rotation" parts to only appear once in a loop. I'm a beginner to this arduino and C code. Thanks for the help or any input
 

heydonms

Member
I'm not familiar with Arduino but for most compilers what you have there isn't a valid program. I suspect the arduino software is trying to be "helpful" by guessing what you really meant and doing that, IMHO it is just teaching bad habits.

You should have a main function, when your program starts it jumps to main from there you call your other functions. Since you don't have one I assume it is probably running one function after another in the order they appear in the source, but unless you can find that documented somewhere it isn't guaranteed. Your functions (other than main) should also end with a return statement. On a PC main also needs to return, on a microcontroller main should never return as it results in a buffer underflow.

Google for a basic C tutorial and look at the program structure. You will probably end up with something like this:

Code:
int main () {
  setup(); // set up IO (maybe put your screen init stuff in there too)

  lcd.begin(16,2); //sets display to start
  lcd.setCursor(0, 0); //sets cursor 1st row
  lcd.print("Booting...");
  delay(1000); //wait for a second
  digitalWrite(BL,HIGH); //turn backlight on

   while ( 1 ) {  // Run the following section for ever
     flashLEDs();
  }
}
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Sorry heydonms but you are wrong... The above program is perfectly fine... The arduino c++ software is event based, you have a "Loop function" and a "setup function"

If you want to run a slice of code once, you put it at the end of the setup function...
 

heydonms

Member
Fair enough, I guess I should refrain from commenting on Arduino stuff if it is that different to normal C.
 

scarygood536

New Member
Thanks a lot! worked like a charm, felt like lcd.begin (16,2); was a redundant code so i put lcd.clear(); in instead... This learning arduino is a slow process. Especially for me not knowing any kind of code. Between the arduino webpage and tronizstuff (I've seen you link to), I've got a lot to learn.
 

misterT

Well-Known Member
Most Helpful Member
This learning arduino is a slow process.
There is nothing to learn. The Arduino stuff is designed for people who don't care how things work. It is good for fast prototyping.. useless for learning anything. If you feel that you are not learning anything, or learning slowly, the reason to that is the Arduino.. not you.
 
Last edited:

Latest threads

EE World Online Articles

Loading

 
Top