Hi all I'm trying to make a led fade in and out by press of a button to turn it on and off and im struggling to try and get code to work together..
code below
My ultimate goal is to be able to press button fade sequence starts and the press button again to turn off fade sequence
// constants will not change
const int buttonPin = 2; // the number of the push button pin
const int ledPin = 3; // the number of the LED pin
const int brightness = 0;
const int fadeAmount = 5;
// variables wants change
int buttonState = 0; // variable for reading the push button status
int ledState = 0; // variable for reading the LED status
int counter = 0; // variable for remember the number of button pressed
// the setup routine runs once you turn the device on or you press reset
void setup ()
{
// initialize the LED pin as an output
pinMode (ledPin, OUTPUT );
// initialize the pushbutton pins as input and enable internal pull-up resistor
pinMode (buttonPin, INPUT_PULLUP );
// initialize serial communication at 9600 bits per second
Serial.begin (9600);
}
// the loop routine runs over and over again forever
void loop ()
{
// read the state of the pushbutton value
int state = digitalRead (buttonPin);
// recognize state changes: button pressed and button released
if (state != buttonState)
{
// remember new button state
buttonState = state;
// print out the state of the button
Serial.print ( "2 State changed" );
Serial.println (buttonState);
// button is pressed and LED is off
if (buttonState == LOW && ledState == LOW )
{
// remember new LED state
ledState = HIGH ;
// turn LED on
//digitalWrite (ledPin, HIGH );
fade();
// print out the state of the LED
Serial.print ( "2 set high" );
Serial.println (ledState);
// increment number of button pressed
counter ++;
// print out the number of button pressed
Serial.print ( "2 counter:" );
Serial.println (counter);
}
// button is pressed and LED is on
else if (buttonState == LOW && ledState == HIGH )
{
// remember new LED state
ledState = LOW ;
// turn LED off
digitalWrite (ledPin, LOW );
// print out the state of the LED
Serial.print ( "2 Set low" );
Serial.println (ledState);
// increment number of button pressed
counter ++;
// print out the number of button pressed
Serial.print ( "2 counter:" );
Serial.println (counter);
}
// button is released
else if (buttonState == HIGH )
{
// print out new line
Serial.println ();
// wait before next click is recognized
delay (100);
}
}
}
void fade() {
// set the brightness of pin 9:
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
What is it not doing that you expect it to do? Keep in mind only you can see what's actually happening so you have to guide responders along so they can help you.
My apologies I'm trying to get this to fade as of now it only comes on and goes off so blink with a button press while I'm trying to get it to fade instead and i can figure out how to make it fade with a press of button
Is the sketch you posted, your attempt to merge two sketches? If so, please also post the two source sketches.
Your explanation of the program operation lacks specific detail, it is possible to make something from it, but there is a good chance it's not the same thing that you want.
So you have to explain yourself better. Take some time to imagine how a person who is not at all familiar with your goals and activities, would interpret whatever you post.
Also please eschew "code talk" until we understand what you actually want the program to do. No, "can I drag and drop this and that and use it as a function to call this or that..."
It should be like,
initially, fade should be disabled
if the button is pressed, a single fade cycle should start and complete before the button will respond again
This isn't what you want, but it's the kind of operational description that we need.
Heard, i have trouble with communication at times i apologies for that im on the spectrum. so please bare with me. i will try my best.
The above sketch and this one below are what im trying to merge
What im trying to do is this...
Im making a LED fade by pressing a button
turn it off by pressing same button or another button( if need be..)
im literally trying to figure out how to make and led come on and off with press of button and fade after i press button and i cant figure out where i need to go next either steps backward to fix code or what..
im trying to make a staff that has an led on the end of it that i can hit a button and the fade sketch on the arduino starts and hit button and make it stop turn off.. and im struggling and i just want to make her happy
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/
int led = 9; // the PWM pin 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);
}
// 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;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
That was the fade sketch. Now please post the button sketch. Test both of them independently and report here whether they work to your satisfaction, or not.
In daily life, humans "fill in the blanks" so that precise, technical language is not necessary. In programming, that doesn't work out so well. You think, "this is a short, complete and clear description". The problem is, it actually leaves a lot of possibilities for interpretation open.
fade once, or continuously?
when the button is held down, or when it is pressed?
should a fade sequence complete, or halt immediately when the stop button is pressed?
should a fade sequence re-start from the beginning if the start button is pressed while it's running?
how long is a fade sequence?
The precise answers to these and possibly other answers, depends on having a precise idea of what you expect it to do.