Go Down

Topic: A question about timing. (Read 438 times) previous topic - next topic

zepp

Hello all. I'm messing with some simple things to get a bit more familiar with this Arduino business.

I have the following code:

Code: [Select]

#include <Wire.h>
#include <Centipede.h>
#include <LCDI2C4Bit.h>

Centipede CS;
int ADDR=0x27;


LCDI2C4Bit lcd = LCDI2C4Bit(ADDR,2,16);

void setup(void){
Wire.begin();
Serial.begin(9600);
CS.initialize();
CS.pinMode(7, OUTPUT);
CS.pinMode(6,OUTPUT);
CS.pinMode(5, OUTPUT);
CS.pinMode(4, OUTPUT);
CS.pinMode(3, OUTPUT);
lcd.init();
lcd.clear();
}

void loop(){
  blinkLED();
  displayonLCD();
}
 
 
void blinkLED(){
   CS.digitalWrite(7, HIGH);
   CS.digitalWrite(5, HIGH);
   CS.digitalWrite(3, HIGH);
   CS.digitalWrite(6, LOW);
   CS.digitalWrite(4, LOW);
   delay(1000);
   CS.digitalWrite(6, HIGH);
   CS.digitalWrite(4, HIGH);
   CS.digitalWrite(7, LOW);
   CS.digitalWrite(5, LOW);
   CS.digitalWrite(3, LOW);
  delay(1000);
}

void displayonLCD(){
  lcd.printIn("Arduino is good");
  lcd.cursorTo(2,8);
  lcd.printIn("for your brain!  ");
  delay(5000);
  lcd.clear();
  lcd.printIn("Tacos!");
  delay(5000);
  lcd.clear();
  lcd.printIn("Burritos!");
  delay(5000);
  lcd.clear();
}


It blinks some LEDs, and displays different messages on an LCD screen. The problem is that with the delay()s in the LCD code the LEDs become so delayed that they don't really blink...they just shift postions when the program loops back around. I assume that timing issues like this might mess with me later when I'm trying to create better things, and I'd like to know if there is any way to fix this?

Thanks. :)
I'm here to learn and help when I'm able :)

Jack Christensen

#1
Aug 18, 2011, 04:33 am Last Edit: Aug 18, 2011, 04:35 am by Jack Christensen Reason: 1
This is sort of a classic problem, basically there are two things we want going on simultaneously, and both have delays in them, which then interfere with each other. I assume you are familiar with the "Blink" example? (In the Arduino IDE, File > Examples > Basics > Blink)

Also check out Examples > Digital > BlinkWithoutDelay ... this blinks an LED exactly like the "Blink" sketch, but uses a very different mechanism that doesn't employ the delay() function.  Understand that concept and I think you will be able to apply it to your sketch.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

zepp

I just reviewed the samples again. I haven't looked at them in a while. The blink without delay solution is so simple/obvious... Thanks!
I'm here to learn and help when I'm able :)

Go Up