Pages: [1]   Go Down
Author Topic: A question about timing.  (Read 395 times)
0 Members and 1 Guest are viewing this topic.
Fredericksburg, VA
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Kooky
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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. smiley
Logged

I'm here to learn and help when I'm able smiley

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3973
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 17, 2011, 09:35:43 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Fredericksburg, VA
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Kooky
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

I'm here to learn and help when I'm able smiley

Pages: [1]   Go Up
Jump to: