Devices & Components
Arduino Nano
Custom 7 seg display
Rotary Encoder
Software & Tools
Arduino IDE
Project description
Code
Controlling custom 7 segment display (shift register driven) with Rotary encoder
arduino
1int DS1_pin = 6; 2int STCP1_pin =9; 3int SHCP1_pin = 8 ; 4int DT_pin = 5; 5 6int counter=0; 7unsigned long last_run=0; 8 9int digits [10][8]{ 10 {0,1,1,1,1,1,1,0}, // digit 0 11 {0,0,1,1,0,0,0,0}, // digit 1 12 {0,1,1,0,1,1,0,1}, // digit 2 13 {0,1,1,1,1,0,0,1}, // digit 3 14 {0,0,1,1,0,0,1,1}, // digit 4 15 {0,1,0,1,1,0,1,1}, // digit 5 16 {0,1,0,1,1,1,1,1}, // digit 6 17 {0,1,1,1,0,0,0,0}, // digit 7 18 {0,1,1,1,1,1,1,1}, // digit 8 19 {0,1,1,1,1,0,1,1} // digit 9 20}; 21 22 23void DisplayDigit(int Digit) 24{ 25 digitalWrite(STCP1_pin,LOW); 26 for (int i = 7; i>=0; i--) 27 { 28 digitalWrite(SHCP1_pin,LOW); 29 if (digits[Digit][i]==1) digitalWrite(DS1_pin, LOW); 30 if (digits[Digit][i]==0) digitalWrite(DS1_pin, HIGH); 31 digitalWrite(SHCP1_pin,HIGH); 32 } 33 digitalWrite(STCP1_pin, HIGH); 34} 35 36void shaft_moved(){ 37 if (millis()-last_run>5){ 38 if (digitalRead(4)==1){ 39 if (counter<9) counter++; 40 } 41 if (digitalRead(4)==0){ 42 if(counter>0) counter--; 43 } 44 last_run=millis(); 45 } 46} 47 48void reset_to_0(){ 49 counter=0; 50} 51 52void setup() { 53 pinMode(DS1_pin, OUTPUT); 54 pinMode(STCP1_pin, OUTPUT); 55 pinMode(SHCP1_pin, OUTPUT); 56 attachInterrupt(digitalPinToInterrupt(3), shaft_moved, LOW); 57 attachInterrupt(digitalPinToInterrupt(2), reset_to_0, FALLING); 58 pinMode(2,INPUT_PULLUP); 59 pinMode(DT_pin,INPUT); 60} 61 62void loop() { 63 DisplayDigit(counter); 64}
Rotary encoder. Limiting the counter range to 5 to 10.
arduino
1int counter=5; 2String dir=""; 3unsigned long last_run=0; 4 5 6void setup() { 7 Serial.begin(9600); 8 attachInterrupt(digitalPinToInterrupt(3), shaft_moved, FALLING); 9 pinMode(4,INPUT); 10} 11 12void shaft_moved(){ 13 if (millis()-last_run>5){ 14 if (digitalRead(4)==1){ 15 if(counter<10) counter++; 16 dir="CW"; 17 } 18 if (digitalRead(4)==0){ 19 if (counter>5) counter--; 20 dir="CCW";} 21 last_run=millis(); 22 } 23} 24 25void loop() { 26 Serial.print("counter : "); 27 Serial.print(counter); 28 Serial.print(" direction : "); 29 Serial.println(dir); 30}
Recognising position and direction of the Rotary Encoder and displaying it in Serial Monitor
arduino
1int counter=0; 2String dir=""; 3unsigned long last_run=0; 4 5 6void setup() { 7 Serial.begin(9600); 8 attachInterrupt(digitalPinToInterrupt(3), shaft_moved, FALLING); 9 pinMode(4,INPUT); 10} 11 12void shaft_moved(){ 13 if (millis()-last_run>5){ 14 if (digitalRead(4)==1){ 15 counter++; 16 dir="CW"; 17 } 18 if (digitalRead(4)==0){ 19 counter--; 20 dir="CCW";} 21 last_run=millis(); 22 } 23} 24 25void loop() { 26 Serial.print("counter : "); 27 Serial.print(counter); 28 Serial.print(" direction : "); 29 Serial.println(dir); 30}
Comments
Only logged in users can leave comments