Seems like this should work....

Hi,

Would really appreciate understanding why this is not working as I think it should.

What I am trying to accomplish is this: When an input goes from low to high, a PWM output pin is held at 0. Then, when the input pin goes back to low, the PWM output sweeps once between two values.

My sketch below performs the sweep too soon, when the input pin goes high.

const int buttonPin = 2;
const int ledPin = 9;
int buttonState = 0;
int lastButtonState = 0;

void setup()

{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT); }

void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) {
analogWrite (ledPin, 0);}
else;
int x = 1;
for (int i = 0; i > -1; i = i + x){
analogWrite(ledPin, i);
if (i == 255) x = -1;
delay(10);
}
}
}

THanks,
Geno

First, fix your indenting. Put each curly brace on it's own line and use the Tools > Auto Format in the Arduino's IDE. Then edit your post, remove the old code, add the nice looking code and surround it with code tags. It's much easier to help you when you follow directions.

Firstly, I don't think this is what you were trying to do:

else;

perhaps this:

else {

and an additional closing '}' at the end of the for loop for the else statement.

You need to update lastButtonState somewhere... (I'll let you find out exactly where as an excercise)

This section is horribly confusing:

if (buttonState != lastButtonState)
  {
        if (buttonState == HIGH) {
       analogWrite (ledPin, 0);}
 else;
  :

Messy code happens for two reasons - (a) one is trying to fix some code and is doing "quick-n-dirty" or "hammer this hard and see if it works". OK as part of a learning/debugging process, but not something you want to show your friends. (b) one has insuffcient knowledge of what the code does. I am not implying either in your case.

I'd rewrite that as

if (buttonState != lastButtonState)
  {
  if (buttonState == HIGH) analogWrite(ledPin, 0);
  :

OK, we can now see that the logic says: When buttons goes high - turn on the LED. On any button change perform the code represented by the ":" - which is what you describe as your problem.

(I see that in the meantime others have pointed out you do not save the new buttonState)

Simple toggle code.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Msquare,

Insufficient knowledge (on my part) would be an understatement!

I have this working now thanks to you and the others who kindly replied.

Thanks,
Geno