/*
Encodeur rotatif et Fading LED
*/
int led1 = 8;
int led2 = 9;
int led3 = A0;
int led4 = 10;
int led5 = 11;
int led6 = 12;
int led7 = 13;
int encoderA = 4;
int encoderB = 5;
int crans = 1;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int nb = 0;
int n = LOW;
int encoder0PinALast = LOW;
int encoder0Pos = 0;
int oldnb = 0;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin (9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode (encoderA, INPUT_PULLUP);
pinMode (encoderB, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop() {
n = digitalRead(encoderA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoderB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
}
encoder0PinALast = n;
if (encoder0Pos > (7 * crans))
{
encoder0Pos = (7 * crans);
}
if (encoder0Pos < 0)
{
encoder0Pos = 0;
}
nb = encoder0Pos / crans;
Serial.print(encoder0Pos);
Serial.print ("-");
Serial.print (nb);
// set the brightness of pin 9:
// analogWrite(led1, brightness);
// analogWrite(led2, brightness);
// analogWrite(led3, brightness);
// analogWrite(led4, brightness);
// analogWrite(led5, brightness);
// analogWrite(led6, brightness);
// analogWrite(led7, brightness);
Serial.print ("=");
Serial.print(oldnb);
Serial.print ("-");
Serial.println (nb);
if (oldnb < nb)
{
fadein(nb);
oldnb = nb;
}
if (oldnb > nb)
{
fadeout(oldnb);
oldnb = nb;
}
// 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);
}
void fadein(int nb)
{
int brightness = 0;
while (brightness < 256)
{
if (nb == 1) {
digitalWrite(led1, 0);
}
if (nb == 2) {
digitalWrite(led2, 0);
}
if (nb == 3) {
digitalWrite(led3, 0);
}
if (nb == 4) {
digitalWrite(led4, 0);
}
if (nb == 5) {
digitalWrite(led5, 0);
}
if (nb == 6) {
digitalWrite(led6, 0);
}
if (nb == 7) {
digitalWrite(led7, 0);
}
delay(16 - brightness / 16);
if (nb == 1) {
digitalWrite(led1, 1);
}
if (nb == 2) {
digitalWrite(led2, 1);
}
if (nb == 3) {
digitalWrite(led3, 1);
}
if (nb == 4) {
digitalWrite(led4, 1);
}
if (nb == 5) {
digitalWrite(led5, 1);
}
if (nb == 6) {
digitalWrite(led6, 1);
}
if (nb == 7) {
digitalWrite(led7, 1);
}
delay(brightness / 16);
brightness = brightness + fadeAmount;
}
}
void fadeout(int nb)
{
int brightness = 255;
while (brightness > 0)
{
if (nb == 1) {
digitalWrite(led1, 1);
}
if (nb == 2) {
digitalWrite(led2, 1);
}
if (nb == 3) {
digitalWrite(led3, 1);
}
if (nb == 4) {
digitalWrite(led4, 1);
}
if (nb == 5) {
digitalWrite(led5, 1);
}
if (nb == 6) {
digitalWrite(led6, 1);
}
if (nb == 7) {
digitalWrite(led7, 1);
}
delay(brightness / 16);
if (nb == 1) {
digitalWrite(led1, 0);
}
if (nb == 2) {
digitalWrite(led2, 0);
}
if (nb == 3) {
digitalWrite(led3, 0);
}
if (nb == 4) {
digitalWrite(led4, 0);
}
if (nb == 5) {
digitalWrite(led5, 0);
}
if (nb == 6) {
digitalWrite(led6, 0);
}
if (nb == 7) {
digitalWrite(led7, 0);
}
delay(16 - brightness / 16);
brightness = brightness - fadeAmount;
}
}
Comments