tylerpeppy
Published © GPL3+

Rotary Controlled LED Row

A circuit that controls the amount of LED's turned on via the value of a rotary encoder.

IntermediateShowcase (no instructions)840
Rotary Controlled LED Row

Things used in this project

Hardware components

LED (generic)
LED (generic)
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
USB-A to B Cable
USB-A to B Cable
×1
Breadboard (generic)
Breadboard (generic)
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

Control code

Arduino
// Define Pins
#define CLK 2
#define DT  3
#define SW  4

#define LED1 9
#define LED2 10
#define LED3 11
#define LED4 12
#define LED5 13
#define LED6 14//Analog 0/A0
#define LED7 15//Analog 1/A1
#define LED8 16//Analog 2/A2
#define LED9 17//Analog 3/A3
#define LED10 18//Analog 4/A4

const int interrupt0 = 0;// Interrupt 0 在 pin 2 上
int count = 0;//Define the count
int lastCLK = 0;//CLK initial value
 
void setup()
{
  pinMode(SW, INPUT);
  digitalWrite(SW, HIGH);
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  attachInterrupt(interrupt0, ClockChanged, CHANGE);//Set the interrupt 0 handler, trigger level change

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(LED10, OUTPUT);
  
  Serial.begin(9600);
}
 
void loop()
{
  if (!digitalRead(SW) && count != 0) //Read the button press and the count value to 0 when the counter reset
  {
    count = 0;
    Serial.print("count:");
    Serial.println(count);
  }
}

void LedOutput(int value) 
{
  // Turn off all LEDs
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
  digitalWrite(LED10, LOW);

  // If encoder value is between 1-10, light up appropriate
  // number of LEDs
  if (value >= 1 || value <= 10) {
    switch(value) {
      case 1: 
        digitalWrite(LED1, HIGH);
        break;
      case 2:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        break;
      case 3:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        break;
      case 4:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        break;
     case 5:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        break;
      case 6:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        break;
      case 7:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        break;
      case 8:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        break;
      case 9:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);      
        break;   
      case 10:
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);     
        digitalWrite(LED10, HIGH);        
        break;           
    }
  }
}

//The interrupt handlers
void ClockChanged()
{
  int clkValue = digitalRead(CLK);//Read the CLK pin level
  int dtValue = digitalRead(DT);//Read the DT pin level
  if (lastCLK != clkValue)
  {
    lastCLK = clkValue;
    count += (clkValue != dtValue ? 1 : -1);//CLK and inconsistent DT + 1, otherwise - 1

    Serial.print("count:");
    Serial.println(count);
    LedOutput(count);
  }
}

Credits

tylerpeppy

tylerpeppy

2 projects • 20 followers

Comments