Components and supplies
Jumper wires (generic)
Rotary Encoder with Push-Button
Breadboard (generic)
OLED Expansion
Arduino UNO
Project description
Code
Arduino IDE Codes
arduino
This code requires some additional libraries
1#include <Wire.h> 2#include <Adafruit_SSD1306.h> 3#include <Adafruit_GFX.h> 4#define OLED_ADDR 0x3C 5Adafruit_SSD1306 display(-1); 6 7#if (SSD1306_LCDHEIGHT != 64) 8#error("Height incorrect, please fix Adafruit_SSD1306.h!"); 9#endif 10 11#if (SSD1306_LCDHEIGHT != 64) 12#error("Height incorrect, please fix Adafruit_SSD1306.h!"); 13#endif 14 15#define encoder0PinA 2 16#define encoder0PinB 3 17volatile unsigned int encoder0Pos = 0; 18 19int valA; 20int valB; 21int valC; 22 23byte clk; 24byte menuCount = 1; 25byte dir = 0; 26bool runState = false; 27 28void setup() { 29 pinMode(6, OUTPUT); 30 pinMode(9, OUTPUT); 31 pinMode(10, OUTPUT); 32 33 Serial.begin(9600); 34 display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); 35 display.display(); 36 display.clearDisplay(); 37 attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2 38} 39 40void loop() { 41 clk = digitalRead(4); 42 ledControl(); 43 menuCheck(); 44 staticMenu(); 45 display.clearDisplay(); 46 delay(50); 47} 48 49void staticMenu() { 50 display.setTextSize(2); 51 display.setTextColor(WHITE); 52 53 display.setCursor(10, 0); 54 display.println("yyRobotic"); 55 //--------------------------------- 56 display.setTextSize(1); 57 display.setCursor(10, 20); 58 display.println("Value A:"); 59 display.setCursor(60, 20); 60 display.println(valA); 61 62 display.setCursor(10, 30); 63 display.println("Value B:"); 64 display.setCursor(60, 30); 65 display.println(valB); 66 67 display.setCursor(10, 40); 68 display.println("Value C:"); 69 display.setCursor(60, 40); 70 display.println(valC); 71 72 display.setCursor(10, 50); 73 display.println("Start:"); 74 display.setCursor(45, 50); 75 if (encoder0Pos > 5 && menuCount == 4) { 76 display.println("Run!"); 77 runState = true; 78 } else { 79 display.println("Idle"); 80 runState = false; 81 } 82 display.setCursor(2, (menuCount * 10) + 10); 83 display.println(">"); 84 85 display.display(); 86} 87 88void ledControl() { 89 if (runState == true) { 90 analogWrite(6, valA); 91 analogWrite(9, valB); 92 analogWrite(10, valC); 93 } 94} 95void menuCheck() { 96 if (clk == LOW && menuCount < 5) { 97 menuCount++; 98 encoder0Pos = 0; 99 } 100 if (clk == LOW && menuCount >= 5) { 101 menuCount = 1; 102 } 103 104 if (menuCount == 1) { 105 valA = encoder0Pos; 106 } 107 if (menuCount == 2) { 108 valB = encoder0Pos; 109 } 110 if (menuCount == 3) { 111 valC = encoder0Pos; 112 } 113} 114 115void doEncoder() { 116 if (digitalRead(encoder0PinA) == HIGH) { 117 if (digitalRead(encoder0PinB) == LOW && encoder0Pos > 0) { 118 encoder0Pos = encoder0Pos - 1; 119 dir = 0; 120 } 121 else { 122 encoder0Pos = encoder0Pos + 1; 123 dir = 1; 124 } 125 } 126 else 127 { 128 if (digitalRead(encoder0PinB) == LOW ) { 129 encoder0Pos = encoder0Pos + 1; 130 dir = 1; 131 } 132 else { 133 if (encoder0Pos > 0) { 134 encoder0Pos = encoder0Pos - 1; 135 dir = 0; 136 } 137 } 138 } 139}
Arduino IDE Codes
arduino
This code requires some additional libraries
1#include <Wire.h> 2#include <Adafruit_SSD1306.h> 3#include <Adafruit_GFX.h> 4#define 5 OLED_ADDR 0x3C 6Adafruit_SSD1306 display(-1); 7 8#if (SSD1306_LCDHEIGHT 9 != 64) 10#error("Height incorrect, please fix Adafruit_SSD1306.h!"); 11#endif 12 13#if 14 (SSD1306_LCDHEIGHT != 64) 15#error("Height incorrect, please fix Adafruit_SSD1306.h!"); 16#endif 17 18#define 19 encoder0PinA 2 20#define encoder0PinB 3 21volatile unsigned int encoder0Pos 22 = 0; 23 24int valA; 25int valB; 26int valC; 27 28byte clk; 29byte menuCount 30 = 1; 31byte dir = 0; 32bool runState = false; 33 34void setup() { 35 pinMode(6, 36 OUTPUT); 37 pinMode(9, OUTPUT); 38 pinMode(10, OUTPUT); 39 40 Serial.begin(9600); 41 42 display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); 43 display.display(); 44 display.clearDisplay(); 45 46 attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2 47} 48 49void 50 loop() { 51 clk = digitalRead(4); 52 ledControl(); 53 menuCheck(); 54 staticMenu(); 55 56 display.clearDisplay(); 57 delay(50); 58} 59 60void staticMenu() { 61 display.setTextSize(2); 62 63 display.setTextColor(WHITE); 64 65 display.setCursor(10, 0); 66 display.println("yyRobotic"); 67 68 //--------------------------------- 69 display.setTextSize(1); 70 display.setCursor(10, 71 20); 72 display.println("Value A:"); 73 display.setCursor(60, 20); 74 display.println(valA); 75 76 77 display.setCursor(10, 30); 78 display.println("Value B:"); 79 display.setCursor(60, 80 30); 81 display.println(valB); 82 83 display.setCursor(10, 40); 84 display.println("Value 85 C:"); 86 display.setCursor(60, 40); 87 display.println(valC); 88 89 display.setCursor(10, 90 50); 91 display.println("Start:"); 92 display.setCursor(45, 50); 93 if (encoder0Pos 94 > 5 && menuCount == 4) { 95 display.println("Run!"); 96 runState = true; 97 98 } else { 99 display.println("Idle"); 100 runState = false; 101 } 102 103 display.setCursor(2, (menuCount * 10) + 10); 104 display.println(">"); 105 106 107 display.display(); 108} 109 110void ledControl() { 111 if (runState == true) 112 { 113 analogWrite(6, valA); 114 analogWrite(9, valB); 115 analogWrite(10, 116 valC); 117 } 118} 119void menuCheck() { 120 if (clk == LOW && menuCount < 5) { 121 122 menuCount++; 123 encoder0Pos = 0; 124 } 125 if (clk == LOW && menuCount 126 >= 5) { 127 menuCount = 1; 128 } 129 130 if (menuCount == 1) { 131 valA 132 = encoder0Pos; 133 } 134 if (menuCount == 2) { 135 valB = encoder0Pos; 136 137 } 138 if (menuCount == 3) { 139 valC = encoder0Pos; 140 } 141} 142 143void 144 doEncoder() { 145 if (digitalRead(encoder0PinA) == HIGH) { 146 if (digitalRead(encoder0PinB) 147 == LOW && encoder0Pos > 0) { 148 encoder0Pos = encoder0Pos - 1; 149 dir 150 = 0; 151 } 152 else { 153 encoder0Pos = encoder0Pos + 1; 154 dir 155 = 1; 156 } 157 } 158 else 159 { 160 if (digitalRead(encoder0PinB) == LOW 161 ) { 162 encoder0Pos = encoder0Pos + 1; 163 dir = 1; 164 } 165 else 166 { 167 if (encoder0Pos > 0) { 168 encoder0Pos = encoder0Pos - 1; 169 170 dir = 0; 171 } 172 } 173 } 174}
Comments