Go Down

Topic: Two loops? (Read 740 times) previous topic - next topic

ReinVO

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: [Select]

#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

88

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

ReinVO

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

PaulS

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().

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

88

#5
Feb 08, 2011, 09:07 pm Last Edit: Feb 08, 2011, 09:10 pm by 88 Reason: 1
"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?   8)

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up