Arduino Coding Issue...figured I'd ask my peer here first

jamstraz

Active Member
So I understand this code, and tweaking it I can get it to work with the Arduino that (finally) came today. Only problem is anytime I try to add a button it doesn't seem to work. I don't know if the code is wrong or I am not wiring it right on the breadboard to test it.

The code is as follows


/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// check to see if we are ready for flash
if (brightness > 150) {
// flash the led
analogWrite(led, 255);
delay(50);
// start the dimming effect
fadeAmount = -fadeAmount ;
Serial.println(brightness);
}
if (brightness <= 0) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}


When I try to add a button to control it ala the code here and put the previous code where the button should control it, it fails.

If anyone can assist here I would be grateful. Coding gave me a headache in college and its starting to now lol. Otherwise I will try to see about joining an Arduino forum.
 
OP's code,... so its readable:

Code:
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
    // declare pin 9 to be an output:
    pinMode(led, OUTPUT);
    Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
    
    // set the brightness of pin 9:
    analogWrite(led, brightness);
    
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
    
    // check to see if we are ready for flash
    if (brightness > 150) {

        // flash the led
        analogWrite(led, 255);
        
        delay(50);
        
        // start the dimming effect
        fadeAmount = -fadeAmount ;
        
        Serial.println(brightness);
    }

    if (brightness <= 0) {
        fadeAmount = -fadeAmount;
    }

    // wait for 30 milliseconds to see the dimming effect
    delay(30);
}
 
OP's code,... so its readable:

Code:
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
    // declare pin 9 to be an output:
    pinMode(led, OUTPUT);
    Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
   
    // set the brightness of pin 9:
    analogWrite(led, brightness);
   
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
   
    // check to see if we are ready for flash
    if (brightness > 150) {

        // flash the led
        analogWrite(led, 255);
       
        delay(50);
       
        // start the dimming effect
        fadeAmount = -fadeAmount ;
       
        Serial.println(brightness);
    }

    if (brightness <= 0) {
        fadeAmount = -fadeAmount;
    }

    // wait for 30 milliseconds to see the dimming effect
    delay(30);
}
I tried to ask in an Arduino forum but was basically told to look at examples. I deleted my account.
 
I dont see ANY code that looks like an attempt at adding a button?

Also.. if you are new, I would suggest learning (early on instead of later after its a habit) to NOT use delay() in your code, and learn about using millis() instead.

Couple things.

In your Arduino IDE:

File >> Examples >> 02. Digital >> BlinkWithoutDelay - to learn about how to use millis() instead of delay().

File >> Examples >> 02. Digital >> StateChangeDetection - to learn about buttons, and detecting state changes.
 
I tried to ask in an Arduino forum but was basically told to look at examples. I deleted my account.

I think that is a fair response. (same as mine I guess).... but I guess you already know about the examples? As that IS the code from the examples section?

Where is your attempt at adding a button/switch?

What is it you want the button/switch to do even?

What does 'didnt seem to work' mean?



They can be a snarky bunch over there at the Arduino forums! lol Thats for sure.
 
I was trying to add a button. The code I posted is functional without the button. When I tried to implement the code from the example "Button" into the code, it doesn't work. Either the LED keeps running and bypasses the button or it doesn't run (which is most likely a failure in my wiring in breadboard.

What I want the button to do is: Press button once-Run Code to brighten LED up from 0 to127 slowly and then immediately jump to 255 brightness and then fade back to 0. One press, fire, done. Pressing it again will launch that code. I don't want to hold the button, just a simple press.

In the meantime I'm watching an Arduino training class because coding was never my strong suit. I got a C in C++, I did great in COBOL with an A, but java was D, Data Structrures and Algorithmic Design was a very low D.... VB 6.0 (dating myself) was a B-.
 
Seriously looking at code makes me remember why I went into support and not programming lol. My eyes just hurt even looking at it :/ But I want my models to do more than just light and I don't want to pay through the nose for a fancy lighting kit. So I'm pretty much stuck coding
 
Maybe something quick and simple like this will get you started:

Code:
// this constant won't change:
const int  buttonPin = 2;    // connected to GND & D2
int led = 9;           // the PWM pin the LED is attached to
int fadeThreshold = 100; //127 originally
int fadeSpeed = 10;

// Variables will change:
int buttonState = 0;         // current state of the button

void setup() {
  // initialize the button pin as a input:
  //pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, HIGH);
  pinMode(buttonPin, INPUT_PULLUP);
 
  // initialize the LED as an output:
  pinMode(led, OUTPUT);
 
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
 
  // if the state has changed, increment the counter
  if (buttonState == LOW) {     
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed");

    for (int i = 0; i <= fadeThreshold; i++) {
      //update led with new analog value (brightness)    
      analogWrite(led, i);
      delay(fadeSpeed);
    }

    //jump to 255 (fully on)
    analogWrite(led, 255);
   
  }else{
    //do nothing
  }
     
  // Delay a little bit to avoid bouncing
  delay(50);
}
 
Maybe something quick and simple like this will get you started:

Code:
// this constant won't change:
const int  buttonPin = 2;    // connected to GND & D2
int led = 9;           // the PWM pin the LED is attached to
int fadeThreshold = 100; //127 originally
int fadeSpeed = 10;

// Variables will change:
int buttonState = 0;         // current state of the button

void setup() {
  // initialize the button pin as a input:
  //pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, HIGH);
  pinMode(buttonPin, INPUT_PULLUP);

  // initialize the LED as an output:
  pinMode(led, OUTPUT);

  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // if the state has changed, increment the counter
  if (buttonState == LOW) {    
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed");

    for (int i = 0; i <= fadeThreshold; i++) {
      //update led with new analog value (brightness)   
      analogWrite(led, i);
      delay(fadeSpeed);
    }

    //jump to 255 (fully on)
    analogWrite(led, 255);
  
  }else{
    //do nothing
  }
    
  // Delay a little bit to avoid bouncing
  delay(50);
}
I'll upload it to the Arduino and see what happens. Have to wire the button back into this though.
 
1586649627103.png

Hoping this is right...
 
Its a thing of beauty and I thank you immensely for it. It will be a basis to go off of for my Pegasus UFO button control and everything else. I just need to read the code to fully understand the variables.
 
In my code.. you only need to have the button connected to GND and D2.. (thats it)

There was a note left for you as well in the code.

* const int buttonPin = 2; // connected to GND & D2

I also (seriously) recommend you NOT study and learn this type of approach.. it breeds bad habits. And if you can start of by learning the right way to to do things... if will help you IMMENSELY when you stand to more advanced things.

When you use a delay(), or are stuck in a loop for that matter, you are basically 'all other code' from executing.

So while in a loop or a stuck in a delay(). nothing else happens. You are detecting if a button is being pressed..etc..etc (unless of course the loop IS checking for the button..but you get my point)..

Learning to use timers/millis and non-blocking functions/loops is the proper way to learn, and will help you advance faster and correctly.

The Arduino IDE, has a BUNCH of example.

I suggest you just upload to your Arduino..
see what they do..... change a few variable values.. and see how it affected the outcome/results..etc..

Have fun! Its a HUGE new world that REALLY makes props 'that much better' IMO..

:)
 
ok, either way it's working the way I want it to. I press the button, the LED ramps up, bright flash and done. Press it again, it repeats. Don't press..doesn't turn on.
 
1586660760190.png

I realize the wires from bread board to LED a bit redundant but when the thing will be wired up correctly to the model's base the bread board will be eliminated.
 
I'm prattling on. I took sleeping pills last night and now I'm hyper because I slept too much......But I suppose I will need to (re)learn coding to do everything I'm looking to do but still, the code I got works EXACTLY how I need it to.
 
This thread is more than 3 years old.

Your message may be considered spam for the following reasons:

  1. This thread hasn't been active in some time. A new post in this thread might not contribute constructively to this discussion after so long.
If you wish to reply despite these issues, check the box below before replying.
Be aware that malicious compliance may result in more severe penalties.
Back
Top