I just wired a dc motor to run using pwm on pin 9 for a tutorial. Once I had accomplished this I thought a fun way to implement other things I had learned would be to set up three leds that would light up one at a time as the speed of the motor increased. I am 99 percent sure the wiring aspect is done correctly as I have been hooking up leds over and over and over. My code is passing verification but upon uploading it my leds never lit up. I will post the coding and any help would be greatly appreciated
int motorPin = 9;
int ledPin = 4;
int ledPin2 = 7;
int ledPin3 = 8;
void setup ()
{
pinMode (motorPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
}
void loop ()
{
for (int i=0; i<=255; i++)
{
analogWrite(motorPin, i);
delay(10);
}
if (motorPin >= 85)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
if (motorPin >= 170)
{
digitalWrite(ledPin2, HIGH);
}
else
{
digitalWrite(ledPin2,LOW);
}
if (motorPin >= 255)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
delay(1000);
for (int i=255; i>=0; i--)
{
analogWrite(motorPin, i);
delay(10);
}
delay(1000);
}
See #7
http://forum.arduino.cc/index.php/topic,148850.0.html
if (motorPin >= 85)
You want to look at the value of "i" not mortorPin
for (int i=0; i<=255; i++)
{
analogWrite(motorPin, i);
delay(10);
if ( i >= 85 )
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// etc. for other leds
}
}
LarryD:
See #7
http://forum.arduino.cc/index.php/topic,148850.0.htmlif (motorPin >= 85)
You want to look at the value of "i" not mortorPinfor (int i=0; i<=255; i++)
{
analogWrite(motorPin, i);
delay(10);
if ( i >= 85 )
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// etc. for other leds
}
}
I tried that at first but when I try to look at i, I get an error message that reads "name lookup of "i" changed for new ISO "for" and using obsolete binding at "i"
if ( i >= 85 )
etc.
This code has to be in the for loop since "i" goes out of scope.
Beautiful! Thank you now I know what that error message means XD.