#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
const int NUM_SLIDERS = 3;
const int analogInputs[NUM_SLIDERS] = {28,27,26};
#define ADJ_PIN 28
#define ADJ_PIN1 27
#define ADJ_PIN2 26
bool isFirst = true;
int analogSliderValues[NUM_SLIDERS];
#define OLED_ADDR 0x3C
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 Display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int i = 0;
int rtest = 0;
int r = 0;
int r1 = 0;
int r2 = 0;
int lastR = 0;
int lastR1 = 0;
int lastR2 = 0;
//ICONS
//https://www.mischianti.org/images-to-byte-array-online-converter-cpp-arduino/
//https://www.pixilart.com/draw#
//----------
// 'Discord', 36x36
const unsigned char discord [] PROGMEM = {
// 'discord', 25x25px
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x80, 0x00, 0x00, 0x3f, 0xff, 0xc0,
0x00, 0x00, 0x3f, 0xff, 0xc0, 0x00, 0x00, 0x7f, 0xff, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xe0, 0x00,
0x00, 0xf8, 0xf1, 0xf0, 0x00, 0x00, 0xf0, 0x70, 0xf0, 0x00, 0x00, 0xf0, 0x60, 0xf0, 0x00, 0x00,
0xf0, 0x60, 0xf8, 0x00, 0x00, 0xf8, 0xf1, 0xf8, 0x00, 0x01, 0xff, 0xff, 0xf8, 0x00, 0x01, 0xff,
0xff, 0xf8, 0x00, 0x00, 0xf1, 0xf8, 0xf0, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x00, 0x08, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
// 'Computer', 11x8px
const unsigned char media [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x30, 0x01, 0x8c, 0x3c, 0x01, 0x8c, 0x3f, 0x01, 0x8c, 0x3f, 0x81, 0x8c, 0x3f, 0xe1,
0x8c, 0x3f, 0xf1, 0x8c, 0x3f, 0xf1, 0x8c, 0x3f, 0xe1, 0x8c, 0x3f, 0x81, 0x8c, 0x3f, 0x01, 0x8c,
0x3c, 0x01, 0x8c, 0x30, 0x01, 0x8c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'Computer random', 11x8px
const unsigned char current [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x3f, 0xff, 0xf8, 0x3d, 0xff, 0x78, 0x7d, 0xff,
0xfc, 0x70, 0x7d, 0xdc, 0x7d, 0xff, 0xfc, 0x3d, 0xff, 0x78, 0x3f, 0xff, 0xf8, 0x1f, 0xff, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};
//----------
// 0-1024 into 0-9, So any small fluctuation from the potentiometer will be cut out for the sliders (so they won't jump)
// Check out ---> https://forum.arduino.cc/t/hysteresis/506190
//----------
uint16_t getOutputLevel( uint16_t inputLevel ) {
inputLevel = 1024 - inputLevel;
const uint16_t margin = 10 ; // +/- 10
const uint16_t numberOfLevelsOutput = 10 ; // 0..9
const uint16_t endPointInput[ numberOfLevelsOutput + 1 ] = { 0, 112, 212, 312, 412, 512, 612, 712, 812, 912, 1023 } ;
const uint16_t initialOutputLevel = 0 ;
static uint16_t currentOutputLevel = initialOutputLevel ;
uint16_t lb = endPointInput[ currentOutputLevel ] ;
if ( currentOutputLevel > 0 ) lb -= margin ; // subtract margin
uint16_t ub = endPointInput[ currentOutputLevel + 1 ] ;
if ( currentOutputLevel < numberOfLevelsOutput ) ub += margin ; // add margin
if ( inputLevel < lb || inputLevel > ub ) {
uint16_t i;
for ( i = 0 ; i < numberOfLevelsOutput ; i++ ) {
if ( inputLevel >= endPointInput[ i ] && inputLevel <= endPointInput[ i + 1 ] ) break ;
}
currentOutputLevel = i ;
}
return currentOutputLevel ;
}
//----------
void setup() {
for (int i = 0; i < NUM_SLIDERS; i++) {
pinMode(analogInputs[i], INPUT);
}
Serial.begin(9600);
Display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Display.clearDisplay();
Display.display();
}
void loop() {
updateSliderValues();
sendSliderValues(); // Actually send data (all the time)
// printSliderValues(); // For debug
for(int i = 0; i < NUM_SLIDERS; i++){
if(analogSliderValues[i] < 17 && isFirst){
//vibrate();
//vibrate();
isFirst = false;
} else if(analogSliderValues[i] > 20 && !isFirst){
isFirst = true;
}
}
//Draw Icons
//----------
//x,y,nameofIcon,w,h,color
Display.drawBitmap(-2, -3, media, 24, 24, WHITE); //r
Display.drawBitmap(-7, 14, discord, 36, 36, WHITE); //r1
Display.drawBitmap(0, 44, current, 23, 23, WHITE); //r2
//----------
// Cuts out fluctuation from the potentiometer, or sliders will bounce up and down if pots are not high quality
//----------
r = getOutputLevel(analogSliderValues[0]);
r = r*10.75;
r1 = getOutputLevel(analogSliderValues[1]);
r1 = r1*10.75;
r2 = getOutputLevel(analogSliderValues[2]);
r2 = r2*10.75;
//----------
if(lastR != r || lastR1 != r1 || lastR2 != r2){
if(r % 10 == 0 || r1 % 10 == 0 || r2 % 10 == 0){
vibrate(100);
lastR = r;
lastR1 = r1;
lastR2 = r2;
}
}
// Draw Sliders
//----------
//Chan1
// Draw Box around slider
Display.drawRoundRect(24, 3, 100, 12, 2, WHITE);
// Draw slider
Display.fillRect(26, 5, r, 8, WHITE);
//Chan2
// Draw Box around slider
Display.drawRoundRect(24, 26, 100, 12, 2, WHITE);
// Draw slider
Display.fillRect(26, 28, r1, 8, WHITE);
//Chan3
// Draw Box around slider
Display.drawRoundRect(24, 50, 100, 12, 2, WHITE);
// Draw slider
Display.fillRect(26, 52, r2, 8, WHITE);
Display.display();
Display.clearDisplay(); //important
delay(10);
}
void updateSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++) {
analogSliderValues[i] = analogRead(analogInputs[i]);
}
}
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < NUM_SLIDERS; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < NUM_SLIDERS - 1) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void printSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++) {
String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
Serial.write(printedString.c_str());
if (i < NUM_SLIDERS - 1) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
void invertSlider(int num) {
for (int i = 0; i < NUM_SLIDERS; i++){
if(num - 1 == analogInputs[i]){
analogSliderValues[i] = 1024 - analogRead(analogInputs[i]);
}
}
}
void vibrate() {
digitalWrite(16, HIGH);
delay(100);
digitalWrite(16, LOW);
}
void vibrate(int num) {
num = num < 50 ? 50 : num;
digitalWrite(16, HIGH);
delay(num);
digitalWrite(16, LOW);
}