After the test program of the electronics desigs week I face this week assignment trying to “re-write” some code from Arduino's exemples.
Basically my goal was to control a Fading LED with a button or a generical human interaction starting fron Blink and Button exemples.
Taking care, being my button connect directly to the PWM Pin , to set the input as digitalWrite
That's the way I programmed the Button to activate the Fading LED:
/*
Fade_button
*/
const int ledPin = 8; // LED connected to digital pin 8
const int buttonPin = 7; // the number of the pushbutton pin
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
//Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
//Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
analogWrite(ledPin, brightness); {
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// wait for 300 milliseconds to see the dimming effect
delay(30);
}
}
else {
analogWrite(ledPin, brightness); {
// reverse the direction of the fading at the ends of the fade:
fadeAmount = -fadeAmount ;
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
I obtained LED fading in and out while the button is pushed.
In my planes the button will be changed with a sensor in the future. So I modified the code to have a softer output, closely related to the pushing action.
That' the way I programmed the LED to “fading in” pushing the button and “fading out” leaving it:
/*
Fading turning off and on.
The circuit:
* LED attached from ANALOG pin 8 to ground.
* BUTTON attached from ANALOG pin 7 to vcc.
Created 12 MARCH 2015
By Li_m0
*/
const int ledPin = 8; // LED connected to digital pin 8
const int buttonPin = 7; // the number of the pushbutton pin
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
//Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
//Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
analogWrite(ledPin, brightness); {
if (brightness < 255) {
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
else {
digitalWrite(ledPin, HIGH);}
}
}
else {
analogWrite(ledPin, brightness); {
if (brightness > 0){
// change the brightness for next time through the loop:
brightness = brightness - fadeAmount;
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
else {
digitalWrite(ledPin, LOW);
}
}
}
}
going further
Next electronic challenge will de-solderer the button and substitute it with a light sensor to in order to control the Led intensity in propositional inverse reproach with external ligth. That will be the first step developing a Indoor Agricultural-device preventing constant light to plants.