Pages: [1]   Go Down
Author Topic: How iterate parts of code at different rates?  (Read 452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello! This is my first day playing with an Arduino and using C/C++. I learned how to make my 16x2 LCD say on the top line:
"Hello, World! This is a test."
and on the bottom line I am trying to display the number of seconds passed (millis()/1000) where the top line scrolls independently of the bottom line. However, both lines update at the same rate. I want to make the top line update (meaning scroll by one character) at a set rate that is independent of the bottom line (which should update every second).

The trouble I have had so far is whenever I use a delay() it forces the bottom LCD line, which comes later in the code, to also wait that same amount of time, so the second line is dependent on what the first line does. Is there a way to have two separate loop() statements that run at different rates?

I also tried coming up with a way that would make the top line change after a set number of milliseconds have passed by, but I don't know how to calculate the delta of a variable.  My programming background is in Garry's Mod with Expression 2, which allows me to set the rate that the chip iterates at and has built-in functions like delta() which gives the change in a variable (or input) since the last iteration.

Any help or advice would be awesome!

Here is the code I am using:
Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
String Message;
String Display;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Message = "Hello, World! This is a test";
  lcd.print(Message);
  delay(1000);
}

void loop() {
  lcd.setCursor(0, 0);
  if (Display.length() <= 16) {
    Display = Message;
    lcd.print(Display);
  } else {
    Display.setCharAt(0,' ');
    Display.trim();
    lcd.print(Display);
  }
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  delay(1000);
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://gammon.com.au/blink
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should take a look at the blinkwithoutdelay example, which shows exactly what you want to do.

Essentially, you store the time that you last did something in a variable. Then you keep checking to see if the current time minus the last time (delta t) is greater than your period. If it is, you store the current time in your lastTime variable and do what you need to do.

With this method, loop() will execute thousands of times per second, so you can add as many things that should run at different periods as you want to.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit: Thanks for the link to the tutorial, I followed it through and applied his method to my code and it works great!

Now how would I be able to show time in seconds with a floating point (with decimal places) if I update the time in intervals other than full seconds?
« Last Edit: March 01, 2012, 12:39:16 am by Blaylock1988 » Logged

Pages: [1]   Go Up
Jump to: