Pages: [1]   Go Down
Author Topic: Two loops?  (Read 563 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,


I'm just starting with Arduino and I'm programming my first little projects which is going pretty fine. Alltough, I've run into a problem at the moment.

What I have; a potentiometer which shows a value from 0 to 100 to my 16x2 lcd. I also have a led attached which only lights up when the value is not 0. Now, this value represents speed. I want the potentiometer to control the speed of a blinking led. Using delay in my led blinking function slows down everything, also my loop.

What is the best way of setting the delay of my blinking led without changing the refreshrate of my display?

This is the code I have already;

Code:
#include <LiquidCrystal.h>

int potPin      = A0;
int potValue    = 0;
int potValueLCD = 0;
int potValueLED = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup(){
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode(13, OUTPUT);
  
}

void loop(){
  
  potValue    = analogRead(potPin);
  
  potValueLCD = map(potValue, 0, 1023, 0, 100);
  printToLCD(potValueLCD);
  
  potValueLED = map(potValue, 0, 1023, 3000, 0);
  setLedDelay(potValueLED);
  
  delay(100);
  
}

// printToLCD - Prints the speed to the LCD

void printToLCD(int speed){
  
  lcd.clear();
  
  if(speed>0){
    lcd.print("Speed:");
    lcd.print(speed);
    lcd.setCursor(0,1);
    lcd.print(getSpeedWord(speed));
  }
  else
  {
    lcd.print("Not active");
  }
}

String getSpeedWord(int speed){
  if(speed<10){
    return "Very slow";
  }
  else if(speed<40){
    return "Slow";
  }
  else if(speed<60){
    return "Normal";
  }
  else if(speed<80){
    return "Fast";
  }
  else if(speed<=100){
    return "Very fast";
  }
  return "Unknown";
}

void setLedDelay(int delayTime){
  
  if(delayTime<2999){
    digitalWrite(13, HIGH);  
  }
  else
  {
    digitalWrite(13, LOW);
  }
  
}


Thanks in advice,
Rein
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 34
Nub
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use PWM, i think it would be easier and effective.
Logged

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

Thanks for your quick reply, PWM looks pretty interesting indeed, however I imagine that would only work for a led. What if I wanted to loop something else, for instance just programming logic?

Anyway, Thanks for your reply!

Rein
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the Blink Without Delay example, to see how to do things that need to happen at fixed intervals, like turning the LED off or on, without using delay().
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 446
Posts: 23872
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PWM would only control the brightness, it would still always blink at the same rate (~500 Hz).

Another way would be to use millis(), see "blink without delay" in the playground.
Basically what you do is read millis() in a big outer loop, when you the time increment you want, you allow something to happen.
For example, I have a loop that checks for when 250mS have gone by, if they have I change the state of a colon display in clock.
If 1 second has gone by I update the seconds digit, and so on.
I then check for any serial inputs, then RF inputs, then restart and if 1/4 has elapsed yet.
So I have 3 things being checked at once.

I have another where I check for inputs every 100uS, looking to see if either of 2 switches closed for 2mS, and if one closed did the other close for 2mS within 40mS of the first.

You could do something similar to make the LED turn on and turn off decision based on how much time went by, versus dumbly sitting in one place waiting for delay to elapse. Then the rest of your code keeps running.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 34
Nub
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"PWM would only control the brightness, it would still always blink at the same rate (~500 Hz)."


Not necessarly, PWM toggles high and low which will turn the led on and off (which gives it the dimming effect). Depending on the speed of the PWM, the led can be turned completely on and off.  PWM is great if you dont want to use/purchase other components than what is provided on the arduino board. Though these components are cheap, why waste the extra time and money?   smiley-cool
« Last Edit: February 08, 2011, 03:10:20 pm by 88 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 446
Posts: 23872
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Without jumping thru a lot of hoops tho, I am pretty sure the PWM frequency is ~500 Hz, and analogWrite(PWM) only changes the width of the pulses.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: