PWM Ramp up

Hello World,

I am new to Arduino and I have a problem I require help with if possible. I have an Arduino Mega 2560 and i am trying to make a PWM Ramp Up. I have and modified the following code from the archives;

#include <Button.h>





Button goButton(7,PULLDOWN);

int ledPin = 13;    // LED connected to digital pin 13
byte  goPos = 0;      //loop counter





void setup()  
{ 
 Serial.begin (9600);
 pinMode(ledPin = 13, OUTPUT);
} 




void loop()  
{ 
  
 if(goButton.isPressed())
{
  if (goPos <= 255) {goPos++;}
  if(goPos > 255) {goPos = 255;}
  analogWrite(ledPin, goPos);
  delay(10);
  Serial.print(goPos, DEC);  
 }
}

This performs most of the function I require, however not exactly.

I require to press the button and hold it, and it ramp up to 255 and stay at 255 while the button is pressed, but if the button is released, return to 0 and await until the button is pressed again.

At the moment if the button is pressed and held, it ramps to 255 and then loops over and over 0 to 255. If the button is released, it stays at the number it was at upon release, 128 for example.

Please help

Kind regards

Luke Lawcock

You increment goPos, then check to see if it’s >255. How can a byte variable EVER be > 255?

Regards,
Ray L.

 pinMode(ledPin = 13, OUTPUT);

You already gave ledPin a value - it's got a good memory, you don't need to repeat yourself.

RayLivingston:
You increment goPos, then check to see if it's >255. How can a byte variable EVER be > 255?

Regards,
Ray L.

I don't know, I am new to this, it is code I took from a 2010 forum post as it is meant to do exactly what I intend to use it for. I think i may have to start from scratch as I need it to ramp up slower than it does.

KR Luke

www.arduino.cc/en/Reference/Byte

--Michael