Problems with an LED and PWM

Hi all,
(Im still learning)
On my way to something bigger Ive been looking around and found this pice of code http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1244372691
It is ment for 3-phase motors but I liked the way it had a dead spot in the middle of the pot and whent forward and backward.
To cut a long story short i hooked up 3 LEDs to the digital pins to see it work, then i got thinking what if i use analogOut and make two LEDs dim. . .
This is where Iv hit a snag, the 'forward' else if statment (> 542) works perfect but in the if > 482 statment it just makes the LED blink randomly.
I fixed it a little by putting in a delay, but its still there if you look hard, plus I would rather no use a delay i can...

Here is my code ( sorry its not all commented yet...) (the thrid LED is just the to show it is going 'forward')

int xPin = 2;                    //Define Pot One

int dir = 9;                    //Define Output Pins
int fwd = 11;
int rev = 10;

int val = 0;                    //Define Variable



void setup()
{
  pinMode(dir, OUTPUT);         //Pin Mode
  pinMode(fwd, OUTPUT);
  pinMode(rev, OUTPUT);
                          }

void loop()
{
  {digitalWrite(13, LOW);}                 
  
  val = analogRead(xPin);

  if (val < 482)        //this part dosent work quite right

    {
      digitalWrite(dir, LOW);
      val = map(val, 0, 482, 255, 0);
      analogWrite(rev, val);
      delay(100);
}

  else if (val > 542)        //this part works great

    {
      digitalWrite(dir, LOW);
      val = map(val, 543, 1023, 0, 255);
      analogWrite(fwd, val);
      delay(100);
      digitalWrite(dir, LOW);
}

      
else                          // this part just does what it looks like
      analogWrite(fwd, 0);
      analogWrite(rev, 0);
      digitalWrite(dir, LOW);
      {digitalWrite(13, HIGH);}  //When we do nothing turn the LED on Pin13 ON 
}

If anyone could help shead some light on this that would be great :slight_smile:

I think the problem here is that you are updating the LEDs so often that you are getting a strobe effect between the degree of updating and the PWM frequency. This is helped by adding the delay.

I think there is a mix up between forward reverse and direction, you shouldn't need all three.

If you look in the last else statment it writes 0 to both PWM outputPins and pulls the third LED LOW as well. .

So the dead section does work corectly.

Even with a delay time of 1000ms it will still blink
But the other output is happy with no delay time..

Even with a delay time of 1000ms it will still blink

Try a bit of debug and use the print statement to see what values you are reading and what values you are writing. That will give you a clue as to what is going on.

[edit]What still blinks? The Pin 13 LED or you motor LED?[/edit]

Ok,
So I did what you said and added some debuging and looking serial data coming back from the arduino everything looks like it should be working fine,

ie.
The potPin read's it full range 0 to 1023
In the < 482 section, the pot goes from 0 to 481 and anologPin 10 goes from 0 to 255 Like it should BUT the LED fill Blink randomly, it will flicker slightly too when the arduino is sending serial data. . .
And the > 542 section works perfectly and always has (it doesnt flicker when sending serial data either). . . .

I have changer some pins around, this is what the code looks like now

int xPin = 0;                    //Define Pot One

int dir = 8;                    //Define Output Pins
int fwd = 9;
int rev = 10;

int val = 0;                    //Define Variable



void setup()
{
  pinMode(dir, OUTPUT);         //Pin Mode
  pinMode(fwd, OUTPUT);
  pinMode(rev, OUTPUT);
  
  Serial.begin(57600);           //Turn on the Serial Port at 57600 bps
                          }

void loop()
{
  {digitalWrite(13, LOW);}                 
  
  val = analogRead(xPin);
  Serial.print("intval, ");
  Serial.println(analogRead(0));
  
  if (val < 482)        //this part dosent work quite right

    {
      digitalWrite(dir, LOW);
      val = map(val, 1, 482, 255, 0);
      analogWrite(rev, val);
      Serial.print("revval, ");
      Serial.println(val);
      delay(100);
}

  else if (val > 542)        //this part works great

    {
      digitalWrite(dir, LOW);
      val = map(val, 543, 1023, 0, 255);
      analogWrite(fwd, val);
      Serial.print("fwdval, ");
      Serial.println(val);
      delay(100);
      digitalWrite(dir, LOW);
}

      
else                          // this part just does what it looks like
      analogWrite(fwd, 0);
      analogWrite(rev, 0);
      digitalWrite(dir, LOW);
      {digitalWrite(13, HIGH);}  //When we do nothing turn the LED on Pin13 ON 
}

Ok looks like iv fixed it somewhat

I changed

else                          
      analogWrite(fwd, 0);
      analogWrite(rev, 0);
      digitalWrite(dir, LOW);
      {digitalWrite(13, HIGH);} 
}

To

else                          
      digitalWrite(fwd, LOW);
      digitalWrite(rev, LOW);
      digitalWrite(dir, LOW);
      {digitalWrite(13, HIGH);} 
}

and it seems to work alot better,
Sadly it still needs the delay(100) in there. Does anyone know who to do this without the need for delay?

Thanks

Sadly it still needs the delay(100) in there.

What happens without the delay? In other words, why is it needed?

BUT the LED fill Blink randomly

Not sure what you mean by 'fill' here.
Can you say which LED is blinking.
You don't need the { } around the pin 13 stuff.

Sorry, it ment to say will blink randomly. . . opps. . .

Its the LED on pin 10 (int rev = 10;),
It will aslo do blink when its assinged to a different PWM pin also.

its odd,

I think you have the if .. then ... else .. structure wrong. Try this:-

int xPin = 0;                    //Define Pot One

int dir = 8;                    //Define Output Pins
int fwd = 9;
int rev = 10;

int val = 0;                    //Define Variable



void setup()
{
  pinMode(dir, OUTPUT);         //Pin Mode
  pinMode(fwd, OUTPUT);
  pinMode(rev, OUTPUT);

  Serial.begin(57600);           //Turn on the Serial Port at 57600 bps
                          }

void loop()
{
  digitalWrite(13, LOW);

  val = analogRead(xPin);
  Serial.print("intval, ");
  Serial.println(analogRead(0));

  if (val < 482)        //this part dosent work quite right

    {
      digitalWrite(dir, LOW);
      val = map(val, 1, 482, 255, 0);
      analogWrite(rev, val);
      Serial.print("revval, ");
      Serial.println(val);
      delay(100);
}

if (val > 542)        //this part works great

    {
      digitalWrite(dir, LOW);
      val = map(val, 543, 1023, 0, 255);
      analogWrite(fwd, val);
      Serial.print("fwdval, ");
      Serial.println(val);
      delay(100);
      digitalWrite(dir, LOW);
}


if (val >= 482 && val <= 542) {                          // this part just does what it looks like
      analogWrite(fwd, 0);
      analogWrite(rev, 0);
      digitalWrite(dir, LOW);
      digitalWrite(13, HIGH);  //When we do nothing turn the LED on Pin13 ON
}

Cool, Ill try that this afternoon,
Im at work now and dont have an arduino with me , ill let you know how it goes,

Thanks Grumpy_Mike