Newbie help looking for a sketch

Sorry , have looked around trying to find the right section to post. Hope this is the right one. I'm trying to find a sketch for a project. Is it Github for that or can I find the code here.
thanks
Scott

Do we get some kind of clue as to the nature of the project.

Usually I start with Google and a few keywords along with 'Arduino'.

If you are using a particular chip or device, you probably want a library to simplify your sketch. Start with Google, the designation of the device, 'library', and 'Arduino'. Most libraries come with examples that are a good basis for a sketch.

1 Like

What project? If this is YOUR project, then you need to write the code!

Sorry wasn't wanting to write it all down if in the wrong section.
I have 2 momentary pushbuttons.
if you momentary press pushbutton 1 the led will switch on to 100% brightness.
If you hold the pushbutton the led will brighten from 0% to 100% depending on how long pushbutton 1 is held.
If you momentary press pushbutton 2 the led will switch off. Holding pushbutton2 dims the led down to 0% depending on how long pushbutton 2 is held
hope this helps
thanks

That is code YOU have to write. Try your best, if you get stuck, come back with your code and ask.

Here is how I would do fade-up for one button. See if you can figure out how to add a second button for fade-down,

unsigned long ButtonPressTime = 0;
const byte ButtonPin = 4;
const byte LEDPin = 9; // PWM pin
const unsigned long FadeTime = 5000;

void setup()
{
  pinMode(ButtonPin, INPUT_PULLUP); // Button from pin to groound
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  int buttonState = digitalRead(ButtonPin);
  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressTime == 0)
      ButtonPressTime = millis();  // Start a timer

    // Fade in over time
    unsigned long elapsedTime = millis() - ButtonPressTime;
    if (elapsedTime > FadeTime)
      analogWrite(LEDPin, 255); // Full brightness
    else
      analogWrite(LEDPin, (elapsedTime * 255) / FadeTime);
  }
  else
  {
    // Button was Released
    if (ButtonPressTime != 0)
    {
      // Was that a short press (but not a contact bounce)?
      unsigned long elapsedTime = millis() - ButtonPressTime;
      if (elapsedTime > 10 && elapsedTime < 100)
      {
        // Short press
        analogWrite(LEDPin, 255); // Full brightness
      }
    }
    ButtonPressTime = 0;
  }
}

Hi John
thanks for the help. I have tried a dimdown but with little success. I tried to adjust your code by dimming down rather than up. The code is below

unsigned long ButtonPressTime = 0;
const byte ButtonPin = 2; // pin 2 for dim down button
const byte LEDPin = 9; // led pin PWM
const unsigned long FadeTime = 5000; // assigns the variable FadeTime 5 seconds





void setup() {
  pinMode(ButtonPin, INPUT_PULLUP);// when button pressed digitalRead will return LOW
  pinMode(LEDPin, OUTPUT);// seting up pin 9 as output

}

void loop()
{
  int buttonState = digitalRead(ButtonPin);// read the value of pin 2 and assign it to the variable buttonstate
  if (buttonState == LOW)// check to see if button is pressed
  {
    ButtonPressTime = millis(); // IF the button has been pressed assign the variable ButtonPresstime the value of millis(). I assume once the arduino has power  it starts this timer?

    unsigned long elapsedTime = millis() - ButtonPressTime; // the variable elapsedTime is assigned millisvalue - buttonPressTimevalue
    if (elapsedTime > FadeTime ) // if elapsed time is greater than 5 seconds
      analogWrite(LEDPin, 0); // then bring the led down to lowest brightness
    else
      analogWrite(LEDPin, FadeTime * 255 / elapsedTime);             // need this value to get less i.e. to dim down
  }

  // button is not pressed
  else
  {
    // button was released
    if (ButtonPressTime != 0) // check to see if the variable ButtonPresstime is not equal to zero i.e. its not being pressed
    {
      unsigned long elapsedTime = millis() - ButtonPressTime; // checking for contact bounce
      if (elapsedTime > 10 && elapsedTime < 100)
        // checking to make sure its a short bounce and not a debounce
      {
        analogWrite(LEDPin, 0 ); // mininum brightness
      }
    }
    ButtonPressTime = 0; // make ButtonPressTime equal to zero

  }

}


As mentioned I'm new to this and have tried to look into the functions you were using but find it hard to follow what exactly is going on. I have added comments as how I see it which is probably wrong.
when the sketch starts the led stays off. Pressing the button momentarily switches it on to 100% brightness and holding the button in does nothing
Thanks
Scott

You changed:

  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressTime == 0)
      ButtonPressTime = millis();  // Start a timer

to

  if (buttonState == LOW)// check to see if button is pressed
  {
    ButtonPressTime = millis(); 

Hello
In short words:
Button1 will dim up the LED
and
Button 2 will dim down LED
Is this the required functionality ?

The code John posted is medium advanced.
To analyse it take example numbers and write down each calculation and the result to see what is happening.

If you are completely new to programming you should learn some basics.
With programming it is the same as free-style-BMS-biking.
If you barely can ride straight forward the next step is not practising back-flips in the halpipe.
You do practice some more basic exercises and you ask specific questions.

So just go through the code again and as soon as a question arises ask this question.

best regards Stefan

1 Like

More like:
Short press on Button 1 turns LED on full.
Long press on Button 1 fades up LED while button is pressed.
Short press on Button 2 turns LED off.
Long press on Button 2 fades down LED while button is pressed.

(Times were not specified so I chose "under 100 milliseconds" for short press and 5 seconds for fade time.)

OOPS. I just realized that I forgot to start the fades from the previous brightness. Here is the corrected sketch with Fade Up and Fade Down buttons:

const byte FadeUpButtonPin = 4;
const byte FadeDownButtonPin = 5;
const byte LEDPin = 9; // PWM pin
const unsigned long FadeTime = 5000;

unsigned long ButtonPressStartTime = 0;
byte Brightness = 0;

void setup()
{
  pinMode( FadeUpButtonPin, INPUT_PULLUP); // Button from pin to ground
  pinMode( FadeDownButtonPin, INPUT_PULLUP); // Button from pin to ground
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  int buttonState;

  // Fade Up
  buttonState = digitalRead(FadeUpButtonPin);
  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressStartTime == 0)
      ButtonPressStartTime = millis();  // Start a timer

    // Fade in over time
    unsigned long elapsedFadeTime = (Brightness * FadeTime) / 255;
    unsigned long buttonTime = millis() - ButtonPressStartTime;
    if (elapsedFadeTime + buttonTime >= FadeTime)
    {
      Brightness = 255;
    }
    else
    {
      Brightness = ((elapsedFadeTime + buttonTime)  * 255) / FadeTime;
    }
    analogWrite(LEDPin, Brightness);
  }
  else // buttonState == HIGH
  {
    // Button was Released
    if (ButtonPressStartTime != 0)
    {
      // Was that a short press (but not a contact bounce)?
      unsigned long buttonTime = millis() - ButtonPressStartTime;
      if (buttonTime > 10 && buttonTime < 100)
      {
        // Short press
        Brightness = 255;
        analogWrite(LEDPin, Brightness); // Full brightness
        ButtonPressStartTime = 0;  // Done with timer
      }
    }
  }

  // Fade Down
  buttonState = digitalRead(FadeDownButtonPin);
  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressStartTime == 0)
      ButtonPressStartTime = millis();  // Start a timer

    // Fade out over time
    unsigned long elapsedFadeTime = (Brightness * FadeTime) / 255;
    unsigned long buttonTime = millis() - ButtonPressStartTime;
    if (buttonTime >= elapsedFadeTime)
    {
      Brightness = 0;
    }
    else
    {
      Brightness = ((elapsedFadeTime - buttonTime)  * 255) / FadeTime;
    }
    analogWrite(LEDPin, Brightness);
  }
  else // buttonState == HIGH
  {
    // Button was Released
    if (ButtonPressStartTime != 0)
    {
      // Was that a short press (but not a contact bounce)?
      unsigned long buttonTime = millis() - ButtonPressStartTime;
      if (buttonTime > 10 && buttonTime < 100)
      {
        // Short press
        Brightness = 0;
        analogWrite(LEDPin, Brightness); // off
        ButtonPressStartTime = 0;
      }
    }
  }
}

@scottygio
1. Make the following (Fig-1) hardware setup. There is a reason (see Step-2) for connecting the LED at DPin-9.

led1sw2x
Figure-1:

2. The brightness of LED will be controlled by feeding variable voltage (0V to 5V) at DPin-9 (Fig-2). This is called modulation. This modulation can be easily created by executing the following ready-made function:

analogWrite(9, 8BitData);

pwm328
Figure-2:

3. Upload the following starting sketch in UNO.

byte x = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
}

void loop()
{
  while (digitalRead(4) == LOW)
  {
    analogWrite(9, x);
    x = x + 50;
    if (x == 255)
    {
      break;
    }
    delay(1000);
  }

  while (digitalRead(5) == LOW)
  {
    analogWrite(9, x);
    x = x - 50;
    if (x == 0)
    {
      break;
    }
    delay(1000);
  }
}

4. Press and hold K1. Check that the brightness of LED is changing.
5. Release K1 when LED is fully bright.
6. Press and hold K2. Check that LED is dimming.
7. Release K2 when LED is fully dimmed.
8. Add other complexities with the starting sketch of Step-3 and test.

1 Like

Yes
Button 1 pressed momentarily will switch on led at full brightness.
Button 2 pressed momentarily will switch off led ( if its already on)
When the led is lit holding Button 2 will dim the led anywhere from 100% to 0% depending on how long the button2 is held
when the led is not lit holding Button 1 will the led anywhere from 0% to 100% depending on how long button1 is held

Would you recommend best way of debugging a circuit? I have looked in the tutorial sections without much joy, even tried Youtube. I tried out the code on a breadboard with an led with mixed results. Seems like either PB can switch the led on.
Any advice would be much appreciated

Hello
Insert Serial.println´s at POI to see what happens.

1 Like

one good way of debugging is serial output to the serial monitor
I wrote two macros with which the codewriting becomes shorter see here
in post #2 there are two dbg () and dbgi()

best regards Stefan

Could you be more specific about what you are seeing? I can't see your sketch, buttons, LEDs, or wiring from here. If you want debug information on Serial Monitor:

const byte FadeUpButtonPin = 4;
const byte FadeDownButtonPin = 5;
const byte LEDPin = 9; // PWM pin
const unsigned long FadeTime = 5000;

unsigned long ButtonPressStartTime = 0;
byte Brightness = 0;

void setup()
{
  Serial.begin(115200);
  delay(200); // Give Serial Monitor time to connect and reset the sketch
  
  pinMode( FadeUpButtonPin, INPUT_PULLUP); // Button from pin to ground
  pinMode( FadeDownButtonPin, INPUT_PULLUP); // Button from pin to ground
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  int buttonState;

  // Fade Up
  buttonState = digitalRead(FadeUpButtonPin);
  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressStartTime == 0)
    {
      ButtonPressStartTime = millis();  // Start a timer
      Serial.println("Fade Up Button: Start of press.");
    }

    // Fade in over time
    unsigned long elapsedFadeTime = (Brightness * FadeTime) / 255;
    unsigned long buttonTime = millis() - ButtonPressStartTime;
    if (elapsedFadeTime + buttonTime >= FadeTime)
    {
      Brightness = 255;
    }
    else
    {
      Brightness = ((elapsedFadeTime + buttonTime)  * 255) / FadeTime;
      Serial.print("Fade Up Button: Fade up to ");
      Serial.println(Brightness);
    }
    analogWrite(LEDPin, Brightness);
  }
  else // buttonState == HIGH
  {
    // Button was Released
    if (ButtonPressStartTime != 0)
    {
      Serial.println("Fade Up Button: Just released");
      
      // Was that a short press (but not a contact bounce)?
      unsigned long buttonTime = millis() - ButtonPressStartTime;
      if (buttonTime > 10 && buttonTime < 100)
      {
        // Short press
        Serial.println("Fade Up Button: Short-press");
        Brightness = 255;
        analogWrite(LEDPin, Brightness); // Full brightness
        ButtonPressStartTime = 0;  // Done with timer
      }
    }
  }

  // Fade Down
  buttonState = digitalRead(FadeDownButtonPin);
  if (buttonState == LOW)
  {
    // Button is Pressed
    if (ButtonPressStartTime == 0)
    {
      ButtonPressStartTime = millis();  // Start a timer
      Serial.println("Fade Down Button: Start of press.");
    }

    // Fade out over time
    unsigned long elapsedFadeTime = (Brightness * FadeTime) / 255;
    unsigned long buttonTime = millis() - ButtonPressStartTime;
    if (buttonTime >= elapsedFadeTime)
    {
      Brightness = 0;
    }
    else
    {
      Brightness = ((elapsedFadeTime - buttonTime)  * 255) / FadeTime;
      Serial.print("Fade Down Button: Fade down to ");
      Serial.println(Brightness);
    }
    analogWrite(LEDPin, Brightness);
  }
  else // buttonState == HIGH
  {
    // Button was Released
    if (ButtonPressStartTime != 0)
    {
      Serial.println("Fade Down Button: Just released");
      // Was that a short press (but not a contact bounce)?
      unsigned long buttonTime = millis() - ButtonPressStartTime;
      if (buttonTime > 10 && buttonTime < 100)
      {
        Serial.println("Fade Down Button: Short-pres");
        // Short press
        Brightness = 0;
        analogWrite(LEDPin, Brightness); // off
        ButtonPressStartTime = 0;
      }
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.