Go Down

Topic: Create blink sequence inside potentiometer mapped value (Read 341 times) previous topic - next topic

JohannesTN

Hello Forum

I'm in trouble again.. This time with the sketch for my DIY fog lights. I've managed to get this code to nearly work as i want:
Code: [Select]
int LEDGreen=5;
int LEDBlue=6;
int LEDRed=3;

int potPin=0;
int val;

void setup(){
  pinMode(LEDRed,OUTPUT);
  pinMode(LEDGreen,OUTPUT);
  pinMode(LEDBlue,OUTPUT); 
}

void loop(){
 
  val=map(analogRead(potPin), 0, 1023, 0, 4);
  if (val<1) {
    analogWrite(LEDRed,0);
    analogWrite(LEDBlue,255);
    analogWrite(LEDGreen,255);   
  } else if (val<2) {
    analogWrite(LEDRed,255);
    analogWrite(LEDBlue,255);
    analogWrite(LEDGreen,0);   
  } else if (val<3) {
    analogWrite(LEDRed,255);
    analogWrite(LEDBlue,0);
    analogWrite(LEDGreen,255);
  } else if (val<4) {
    analogWrite(LEDRed,0);
    analogWrite(LEDBlue,0);
    analogWrite(LEDGreen,0);   
  }
  delay(10);
}

It works very great, and the color of the RGB's changes as i turn the potentiometer. Here is what i want to do: I would like to be able to create a blink sequence in let's say this part of the sketch (looking something like this, maybe):
Code: [Select]
if (val<1) {
    start:
    delay(1000);
    analogWrite(LEDBlue, high);
    delay(1000);
    analogWrite(LEDBlue, low);
    goto, start   
  } else if (val<2) {
But how do i make it work as i want to? - it will have to cycle from "start" to "goto, start" until the pot value change. I think I have to use whileloop, or the forloop command. If that's right, then how to do it the right way?

I would really like an example or a good hint, to get me in the right direction.

Thank you very much

best regards

PaulS

First, if you use goto, I'm going to come kick your ass.

A while loop will do things over and over, as long as some condition is true (like the analogRead value mapped to that range is the same as the start condition).

Second, delay() is NOT your friend. Read, understand, and embrace the blink without delay example, and you'll see that the while loop isn't even necessary.

johncc


Hello Forum

I'm in trouble again..

Not really.  That is a nicely written sketch, kudos!

You might could change from using if <1 <2 <3 < 4 to using a switch case:0 case:1 case:2 case:3.  But that's not important now https://www.youtube.com/watch?v=VOmD-xqK2Es

Quote

It works very great, and the color of the RGB's changes as i turn the potentiometer. Here is what i want to do: I would like to be able to create a blink sequence in let's say this part of the sketch (looking something like this, maybe):



Study Blink Without Delay.  http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Ok, PaulS beat me to it.  So, "like he said"

Cheers,
John

robtillaart


Something like this, not tried, no goto's
Code: [Select]

int LEDGreen = 5;
int LEDBlue = 6;
int LEDRed = 3;

int potPin = 0;

void setup()
{
  pinMode(LEDRed, OUTPUT);
  pinMode(LEDGreen, OUTPUT);
  pinMode(LEDBlue, OUTPUT); 
  color(0, 0, 0);
}

void loop()
{
  int val = analogRead(potPin) / 256;  // simpler than map

  switch(val)
  {
    case 1: color(0, 255, 255);
      break;
    case 2: color(255, 0, 255);
      break;
    case 3: randomLed();
      break;
    case 0: blink();
      break;
  }
}

void blink()
{
  static unsigned long lastTime = 0;
  static bool ledOn = false;
  if (millis() - lastTime > 500)
  {
     lastTime = millis();
     ledOn = !ledOn;
     if (ledOn) color(255, 255, 255);
     else color(0, 0, 0);
  }
}

void randomLed()
{
  color(random(255), random(255), random(255));
}

void color(int r, int g, int b)
{
  analogWrite(LEDRed, r);
  analogWrite(LEDBlue,b);
  analogWrite(LEDGreen, g); 

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

JohannesTN

Johncc:
I have to say that i nailed the basic sketch somewhere... :$
Actually i got a (nearly) working sketch, using the "switch case" as you suggested, thanks man.
This is my sketch as it looks now:
Code: [Select]
const int min = 0;      // Lowest reading at analog pin
const int max = 1023; // Highest reading at analog pin
int R1 = 3;
int G1 = 5;
int B1 = 6;
int R2 = 9;
int G2 = 10;
int B2 = 11;


void setup()
{
pinMode(R1, OUTPUT);
pinMode(G1, OUTPUT);
pinMode(B1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(G2, OUTPUT);
pinMode(B2, OUTPUT);
analogWrite(R1, 255);
analogWrite(G1, 255);
analogWrite(B1, 255);
analogWrite(R2, 255);
analogWrite(G2, 255);
analogWrite(B2, 255);
}

void loop()
{
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, min, max, 0, 3);
// do something different depending on the
// range value:

switch (range) {

case 0:    //Potentiometer turned between 0-25%
analogWrite(B1, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B1, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B1, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B1, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 
 delay(100);
 
 analogWrite(B2, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 
 delay(100);
break;

case 1:    //Potentiometer turned upto 26-50%
analogWrite(R1, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(R1, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(R1, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(R1, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 
 delay(100);
 
 analogWrite(B2, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 0);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 delay(50);
 analogWrite(B2, 255);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 
 delay(100);
break;

case 2:     //Potentiometer turned upto 51-75%
break;

case 3:   //Potentiometer turned upto 76-100%
break;

}

delay(100);        // delay in between reads for stability

}
Yeah! it's really a mess, I know. And i know I'll have to use the blinkwithoutdelay thingy. When i try to compile my code i get a error at line 5, telling me this:
Quote
sketch_jan28a:5: error: expected unqualified-id before numeric constant
I can't seem to solve the problem myself, but i guess that it is kind of easy to solve... Is it possible to combine some of the analogwrite, and pinmodes? - just to clear some lines, guess that will take up less space on my chip also?

Thank you very much to both of you!

best regards

JohannesTN

robtillaart:
Thank you very much, looks wonderful. I will try to implement that in my own sketch, to clear it out a bit.

AWOL

#6
Jan 28, 2013, 10:24 pm Last Edit: Jan 28, 2013, 10:29 pm by AWOL Reason: 1
Code: [Select]
int B1 = 6;
Unfortunately, B1 is a macro representing the binary constant 1.
Try changing to "blue1" or similar.

There's a rule that names starting with capitals are reserved for macros.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JohannesTN

AWOL:
Oh, i didn't know that, problem solved :-) thanks

I will try to build my sketch around robtillaart's example, to clear it out a bit.

PaulS

Quote
There's a rule that names starting with capitals are reserved for macros.

There's a convention, not a rule, that all capital letter names are constants.

johncc


Actually i got a (nearly) working sketch, using the "switch case" as you suggested, thanks man.


You're welcome, but the important part is learning, figuring-out, out to get rid of the delays!  Really.

Go Up