Hi
I am very new to Arduino, but loving it
I am trying to create a multi level system for LED lighting. I will have 4 push-buttons wired to the Arduino, and when pressed they will light a corresponding LED through a digital out on the Arduino.
That outputs will also go through blocking diodes into potentiometers so as an output voltage between 0 and 5 can be set. This will then go into the AO input.
I am mapping this to the PWM pin 6 to give a corresponding dimming level
Upto this point all is working well, and the circuit responds as expected.
What i want to add is a fade between the levels. Direct on and to the level is too harsh, however i still want to retain the "straight PWM LED" as a reference .
I have included a LEDFader library from github, as i thought i would re purpose some existing code.
i set the LED to a value of 20 to start, and then reference the LEDValue to the outputValue, as there is already a level generated from that output.
I would very much appreciate any help at all, as i would love to be able to complete this project just using Arduino without the need for analogue electronics in the circuit.
here is my code
#include <LEDFader.h>
// set button and LED pin numbers:
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int ledPin1 = 8; // the number of the LED pin
const int ledPin2 = 9;
const int ledPin3 = 10;
const int ledPin4 = 12;
const int analogInPin = A1; // analog in pin
const int analogOutPin = 6; // PWM LED pin
LEDFader led = LEDFader (11); // fading led pin
int sensorValue = 0;
int outputValue = 0;
const int LEDValue = (25, 3000); //fade to 25 over 3 seconds initally
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// setup serial link
Serial.begin(9600);
// initialize the LED pins as an outputs:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pins as an inpust: remember to have 10K to GND and button to 5V
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
// initial ouptput state Level 0
digitalWrite(ledPin1, HIGH);
// set fading led to a value over 2 seconds
led.fade(LEDValue, 2000);
}
void loop(){
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton 1 is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED 1 on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}
{
buttonState = digitalRead(buttonPin2);
// check if the pushbutton 2 is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED 2 on:
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}
buttonState = digitalRead(buttonPin3);
// check if the pushbutton 3 is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED 3 on:
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
}
{
buttonState = digitalRead(buttonPin4);
// check if the pushbutton 4 is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED 4 on:
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
{
sensorValue = analogRead(analogInPin); // get value from the analog in pin
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255); // 0 = off 865 is max as going through diodes so 5- 0.7 is 4.3 (860 + 5 for safety)
LEDValue = outputValue;
analogWrite(analogOutPin, outputValue); // output outputValue to analogueOutPin
Serial.print("sensor = " ); // read sensor in and output to serial
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
led.update ();
}
}
}