Hey there, i'm trying to get this code to work on my arduino. its strange because it was working earlier, then all of a sudden it isnt reading the pot value. I have tried this on an uno and a duemilanove. I've tested other sketches and they are working fine.
Here is the code:
const int LED = 3;
int pot = A4;
void setup()
{
//pinMode (pot, INPUT);
Serial.begin(9600);
}
void loop()
{
float in, out;
float val = analogRead(pot);
//float val = analogRead(0);
val = map(val, 0, 1023, 0, 1000.000);
//analogWrite(pot, val);
Serial.print('potval ');
Serial.println(val);
for (in = 0; in < 6.283; in = in + (.0001 + val/100000))
{
out = sin(in) * 127.5 + 127.5;
analogWrite(LED,out);
Serial.print('ledOut ');
Serial.println(out);
}
}
And here is what its displaying on the serial monitor.
Any idea why this code isn't functioning properly? like i mentioned, other code is compiling and running fine. this code uploads fine but it doesn't react to a potentiometer and isn't printing out anything near what i am specifying it to print in the serial monitor.
@James C4S, the data-type issue is a reasonable theory and certainly a valid concern but it is not the cause of @Drc3p0's problem. This sketch illustrates that map works as expected when out_max is a floating-point constant...
void setup( void )
{
Serial.begin(9600);
}
void loop( void )
{
float val; // = analogRead(pot);
int ar;
for ( ar=0; ar <= 1023; ++ar )
{
val = ar; // = analogRead(pot);
val = map(val, 0, 1023, 0, 1000.000);
Serial.print( ar );
Serial.write( '\t' );
Serial.println( val );
}
while ( true );
}
Changing the serial potval to use "potVal" instead of 'potval' cleaned up the serial monitor! Strange because both ways of indicating makes the text turn to blue, indicating a recognized function. Strange that that would affect it in such a way that it would prevent it from uploading successfully. I was having an issue with Arduino saying that it had uploaded sucessfully, then discovering that it hadn't erased the previous sketch at all, and was just performing the old sketch that was already on the board from a previous experiment.
This discussion does raise a good point though. I did not know that the map function couldn't return a decimal #. How would i get it to return a decimal number to the 2nd power (however you say that properly.) I want to get values that range from .001 to .1
how would I go about obtaining that range of values?
int pot = A4;
void setup()
{
//pinMode (pot, INPUT);
Serial.begin(9600);
}
void loop()
{
float in, out;
float val = analogRead(pot);
//float val = analogRead(0);
A glance at you code (above) makes it look like you where using A0 and are now using A4. Look at the value of "pot" and the commented out analogRead(). Which analog input do you have the pot wired to?
I changed the analog out and Led pins according to what boards I was testing with. I have shields already built for testing from different experiments.
Drc3p0:
This discussion does raise a good point though. I did not know that the map function couldn't return a decimal #.
Yeah, map needs some work.
How would i get it to return a decimal number to the 2nd power (however you say that properly.) I want to get values that range from .001 to .1
how would I go about obtaining that range of values?
Give this a try...
#define bettermap( value, fromLo, fromHi, toLo, toHi ) \
({ \
typeof (value) _value = (value); \
typeof (fromLo) _fromLo = (fromLo); \
typeof (fromHi) _fromHi = (fromHi); \
typeof (toLo) _toLo = (toLo); \
typeof (toHi) _toHi = (toHi); \
(value - fromLo) * (toHi - toLo) / (fromHi - fromLo) + toLo; \
})
const int LED = 3;
const int pot = A4;
void setup()
{
//pinMode (pot, INPUT);
Serial.begin(9600);
}
void loop()
{
float in, out;
float val = analogRead(pot);
//float val = analogRead(0);
val = bettermap(val, 0, 1023, 0, 0.1);
//analogWrite(pot, val);
Serial.print('potval ');
Serial.println(val);
for (in = 0; in < 6.283; in = in + (.0001 + val/100000))
{
out = sin(in) * 127.5 + 127.5;
analogWrite(LED,out);
Serial.print('ledOut ');
Serial.println(out);
}
}
Drc3p0:
Changing the serial potval to use "potVal" instead of 'potval' cleaned up the serial monitor! Strange because both ways of indicating makes the text turn to blue, indicating a recognized function.
I totally ignore the colours that the IDE changes things to. This is recognized:
while (true)
{ }
But it won't do much useful, unless you want your code to stop. The colouring doesn't prove much.
I use that fairly often. It's usually when I want to check the hardware (pin voltages, postion of a servo, whatever) at a particular point in the code.
The values A1, A2 ... are there to use the analog input pins as digital input/output pins with pinMode, digitalRead, digitalWrite.
Just use the plain pin number with analogRead.
Ah yes, you are right - after reading again very carefully the docs, I have to come to the conclusion that the pin mapping is way more complicated than I thought.
My mistake !