Go Down

Topic: button to change my LEDs  (Read 123 times) previous topic - next topic

thefamily

hello. im trying to get my arcade LEDs to stop fading and stay on when i push a button. im very new to this stuff so go easy on me....lol... this is my code and cant seem to get it to work.



#include "SoftPWM.h"
#include <Pushbutton.h>

int count = 0;

#define button1 A1

Pushbutton pbutton1(button1);


void setup()
{
  // Initialize
  SoftPWMBegin();

  // Create and set pin 13 to 0 (off)
  SoftPWMSet(13, 0);
  SoftPWMSet(12, 0);
  SoftPWMSet(11, 0);
  SoftPWMSet(10, 0);
  SoftPWMSet(9, 0);
  SoftPWMSet(8, 0);
  SoftPWMSet(7, 0);
  SoftPWMSet(6, 0);
  SoftPWMSet(5, 0);
  SoftPWMSet(4, 0);
  SoftPWMSet(3, 0);
  SoftPWMSet(2, 0);



          
  // Set fade time for pin 13 to 100 ms fade-up time, and 500 ms fade-down time
 
SoftPWMSetFadeTime(13, 3000, 3000);
SoftPWMSetFadeTime(12, 3000, 3000);
SoftPWMSetFadeTime(11, 3000, 3000);
SoftPWMSetFadeTime(10, 3000, 3000);
SoftPWMSetFadeTime(9, 3000, 3000);
SoftPWMSetFadeTime(8, 3000, 3000);
SoftPWMSetFadeTime(7, 3000, 3000);
SoftPWMSetFadeTime(6, 3000, 3000);
SoftPWMSetFadeTime(5, 3000, 3000);
SoftPWMSetFadeTime(4, 3000, 3000);
SoftPWMSetFadeTime(3, 3000, 3000);
SoftPWMSetFadeTime(2, 3000, 3000);
  Serial.begin(9600);
}
 
void loop()
{
  // Turn on - set to 100%
  SoftPWMSetPercent(13, 100);
  SoftPWMSetPercent(12, 100);
  SoftPWMSetPercent(11, 100);
  SoftPWMSetPercent(10, 100);
  SoftPWMSetPercent(9, 100);
  SoftPWMSetPercent(8, 100);
  SoftPWMSetPercent(7, 100);
  SoftPWMSetPercent(6, 100);
  SoftPWMSetPercent(5, 100);
  SoftPWMSetPercent(4, 100);
  SoftPWMSetPercent(3, 100);
  SoftPWMSetPercent(2, 100);
 
 
 

  // Wait for LED to turn on - you could do other tasks here
  delay(2000);
  if (pbutton1.isPressed()){
      count = count + 1; //increase the counter once the button is released
      delay(1000);
      Serial.println(count);
    }
if (count == 0) {

  // Turn off - set to 0%
  SoftPWMSetPercent(13, 0);
  SoftPWMSetPercent(12, 0);
  SoftPWMSetPercent(11, 0);
  SoftPWMSetPercent(10, 0);
  SoftPWMSetPercent(9, 0);
  SoftPWMSetPercent(8, 0);
  SoftPWMSetPercent(7, 0);
  SoftPWMSetPercent(6, 0);
  SoftPWMSetPercent(5, 0);
  SoftPWMSetPercent(4, 0);
  SoftPWMSetPercent(3, 0);
  SoftPWMSetPercent(2, 0);
 
  // Wait for LED to turn off
  delay(2000);
 
 if (count == 2){
  count = 0;
 }



      
}    }

i want them to always fade but when im playing a game i want them on but not fading. any help would be greatly appreciated .thanks!

Semtex9

#1
Oct 08, 2020, 08:02 pm Last Edit: Oct 08, 2020, 08:23 pm by Semtex9
check fade without delay code on tutorial, it uses analog write.
If you just tell the pin what to do line by line its gonne do just that, you need to set the condition of when button is pressed vs when its released, or you can create 2 while loops and toggle between them?

if (pbutton1.isPressed()){
     count = count + 1; //increase the counter once the button is released
what it means is once its pressed

I never faded them like this

TomGeorge

#2
Oct 09, 2020, 01:08 am Last Edit: Oct 09, 2020, 01:08 am by TomGeorge
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What does your code do, what is the problem?

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up