I am trying to write a piece of code that varies the blink rate controlled by a potentiometer between 0.1 and 10 hz. i have wrote numerous piece of code and the blink rate just isn't right and is always a few seconds out. Could someone please point me in the right direction.
I am not allowed to use the map function.
thanks in advance
the blink rate of an LED*
Show us your sketch so far.
#define LED1 5 // LEDs on pins 5&6
#define POT1 0 //pots on 0£1
boolean ledState1; // ledState used to set the LED -
unsigned long int previousMillis1, , Now, interval1, , potval1, ;
void setup() {
pinMode(LED1, OUTPUT);
}
void loop()
{
potval1=analogRead(POT1); //read the pots
interval1=((4950*potval1)>>10)+50; //this is the y=mx+c mapping stuff
Now = millis(); //read the free running millisecond timer
if(Now - previousMillis1 >= interval1) {
previousMillis1 = Now;
digitalWrite(LED1, ledState1^=1);
}
}
Duplicate post deleted
interval1 = 103400/(potval1+10)
CHANGE:
unsigned long int previousMillis1, , Now, interval1, , potval1, ;
TO:
unsigned long previousMillis1;
unsigned long Now;
unsigned long interval1;
unsigned long potval1;
Maybe look at this.
Then modify it to your needs:
#define LED1 5 // LEDs on pins 5&6
#define POT1 0 //pots on 0£1
boolean ledState1; // ledState used to set the LED -
unsigned long previousMillis1;
unsigned long Now;
unsigned long interval1;
unsigned long potval1;
void setup() {
pinMode(LED1, OUTPUT);
}
void loop()
{
potval1=analogRead(POT1); //read the pots
potval1 = map(potval1,0,1023,100,10000); //100 ~ .1ms and 10000 ~ 10sec
Now = millis(); //read the free running millisecond timer
if(Now - previousMillis1 >= potval1) {
previousMillis1 = previousMillis1 + potval1;
digitalWrite(LED1,ledState1 );
ledState1 = !ledState1;
}
}// END of loop()
Thanks for all the help guys
none of these fix the problem
the first reply doesn't make it blink correct
changing the way they are declared just makes them blink for 7 seconds constantly with no variation
and even though the last idea is great i have already done this and i don't want to use the map function sorry.
I am not allowed to use the map function.
That's OK, we'll let you.
Just promise you won't cross-post again.
interval1=((4950*potval1)>>10)+50;
That doesn't look very useful.
7 seconds constantly
How is your POT wired?
Put a Serial.println (potval1); to see if your POT is changing the analog input.
ok i wont do and i just want to figure out how to do it without the map function
the pot is changing the input ![]()
eh this is confusing me so much now, going to keep working at it
all your help is appreciated
What happened when you did this?
#define LED1 5 // LEDs on pins 5&6
#define POT1 0 //pots on 0£1
boolean ledState1; // ledState used to set the LED -
unsigned long previousMillis1;
unsigned long Now;
unsigned long interval1;
unsigned long potval1;
void setup() {
pinMode(LED1, OUTPUT);
}
void loop()
{
potval1=analogRead(POT1); //read the pots
interval1=((4950*potval1)>>10)+50; //this is the y=mx+c mapping stuff
Now = millis(); //read the free running millisecond timer
if(Now - previousMillis1 >= interval1) {
previousMillis1 = Now;
digitalWrite(LED1, ledState1^=1);
}
}
What happens when you do this?interval1=((4950*potval1)>>10)+50;
(I already dropped a hint)
it just does the same as it did before the led is at about 0.1 hz to 5hz
i think the problem is somewhere here: interval1=((4950*potval1)>>10)+50;
but i don't know i calculated it and it should work in theory....
I am not allowed to use the map function.
Make your own: ![]()
unsigned long myMap(int x, int in_min, int in_max, unsigned long out_min, unsigned long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
i think the problem is somewhere here: interval1=((4950*potval1)>>10)+50
I wonder what makes you think that (replies 9 and 14, perhaps?)
it should work in theory....
Theory says bumblebees can't fly.
Maybe theory is wrong, and practice and reality are really important.
it just does the same as it did before the led is at about 0.1 hz to 5hz
i think the problem is somewhere here: interval1=((4950*potval1)>>10)+50;
but i don't know i calculated it and it should work in theory....
Aren't you curious what potval1 is when the pot is adjusted to minimum travel and what potval1 is when the pot is adjusted to maximum?
Your Arduino knows ... but it's not talking.
Requirement:
You ask it in your code, then re-program the Arduino.
Open the serial monitor, adjust the pot and get the answer(s).
Do the displayed values make sense?
If so, try a manual calculation with your formula to see what you get.
If not, check your circuit.
Or, just generate the values and immediately see what happens when you use 16 bit signed integer arithmetic.