#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
//#include <FastLED.h> <-- dont need this for now
//expanded to 6 knobs/sliders from 4 on basis on the Deej Demo: https://wokwi.com/projects/392822964817731585
//and without fastLEDs
//#define LED_PIN 13
//#define NUM_LEDS 4
//#define LED_TYPE WS2811
//#define COLOR_ORDER RGB
//CRGB leds[NUM_LEDS];
//Potentiometer:
const int potPin1 = A0;
const int potPin2 = A1;
const int potPin3 = A2;
const int potPin4 = A3;
const int potPin6 = A6;
const int potPin7 = A7;
#define ADJ_PIN A0
#define ADJ_PIN1 A2
#define ADJ_PIN2 A1
#define ADJ_PIN3 A3
#define ADJ_PIN6 A6
#define ADJ_PIN7 A7
const int NUM_KNOBS = 6;
const int analogInputs[NUM_KNOBS] = {A0, A1, A2, A3, A6, A7};
int analogSliderValues[NUM_KNOBS];
#define OLED_RESET 4
Adafruit_SSD1306 Display(OLED_RESET);
int i = 0;
int rtest = 0;
int r = 0;
int r1 = 0;
int r2 = 0;
int r3 = 0;
int r4 = 0;
int r5 = 0;
//ICONS
//https://www.mischianti.org/images-to-byte-array-online-converter-cpp-arduino/
//https://www.pixilart.com/draw#
//----------
// 'Atari game', 11x8px
const unsigned char art11x8 [] PROGMEM = {
0x20, 0x80, 0x11, 0x00, 0x3f, 0x80, 0x6e, 0xc0, 0xff, 0xe0, 0xbf, 0xa0, 0xa0, 0xa0, 0x1b, 0x00
};
// 'Discord', 11x8px
const unsigned char discord [] PROGMEM = {
0x31, 0x80, 0x60, 0xc0, 0x7f, 0xc0, 0xee, 0xe0, 0xee, 0xe0, 0xff, 0xe0, 0x7f, 0xc0, 0x31, 0x80
};
// 'Computer', 11x8px
const unsigned char computer [] PROGMEM = {
0x7f, 0xc0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7f, 0xc0, 0x04, 0x00, 0x0e, 0x00
};
// 'Computer random', 11x8px
const unsigned char computerrandom [] PROGMEM = {
0xff, 0xe0, 0xd5, 0x60, 0xaa, 0xa0, 0xd5, 0x60, 0xaa, 0xa0, 0xff, 0xe0, 0x04, 0x00, 0x0e, 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 ) {
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() {
// LED
//delay(300);
//FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
//FastLED.setBrightness(255); // Initial brightness (0 to 255)
// LED
Serial.begin(9600);
Display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Validate Display address first!
Display.clearDisplay();
Display.display();
for (int i = 0; i < NUM_KNOBS; i++) {
pinMode(analogInputs[i], INPUT);
}
}
void loop() {
// LED
//int brightnessValue1 = analogRead(potPin1); // Read potentiometer value for LED at index 0
//int mappedBrightness1 = map(brightnessValue1, 0, 1023, 0, 255); // Map it to FastLED brightness range
//int brightnessValue2 = analogRead(potPin2); // Read potentiometer value for LED at index 1
//int mappedBrightness2 = map(brightnessValue2, 0, 1023, 0, 255); // Map it to FastLED brightness range
//int brightnessValue3 = analogRead(potPin3); // Read potentiometer value for LED at index 1
//int mappedBrightness3 = map(brightnessValue3, 0, 1023, 0, 255); // Map it to FastLED brightness range
//int brightnessValue4 = analogRead(potPin4); // Read potentiometer value for LED at index 1
//int mappedBrightness4 = map(brightnessValue4, 0, 1023, 0, 255); // Map it to FastLED brightness range
//CRGB customColor = CRGB(201, 0, 102);
//leds[0] = customColor;
//leds[0].nscale8_video(mappedBrightness1);
//leds[1] = customColor;
//leds[1].nscale8_video(mappedBrightness2);
//leds[2] = customColor;
//leds[2].nscale8_video(mappedBrightness3);
//leds[3] = customColor;
//leds[3].nscale8_video(mappedBrightness4);
//FastLED.show();
// LED
updateSliderValues();
sendSliderValues();
delay (20);
//Draw Icons
//----------
//x,y,nameofIcon,w,h,color
Display.drawBitmap(0, 0, computer, 11, 8, WHITE);
Display.drawBitmap(0, 18, discord, 11, 8, WHITE);
Display.drawBitmap(69, 0, art11x8, 11, 8, WHITE);
Display.drawBitmap(69, 18, computerrandom, 11, 8, WHITE);
//----------
// Cuts out fluctuation from the potentiometer, or sliders will bounce up and down if pots are not high quality
//----------
r = getOutputLevel(analogRead(ADJ_PIN));
r = r * 4.5;
r1 = getOutputLevel(analogRead(ADJ_PIN1));
r1 = r1 * 4.5;
r2 = getOutputLevel(analogRead(ADJ_PIN2));
r2 = r2 * 4.5;
r3 = getOutputLevel(analogRead(ADJ_PIN3));
r3 = r3 * 4.5;
//----------
// Draw Sliders
//----------
//Chan1
// Draw Box around slider
Display.drawRoundRect(14, 0, 44, 8, 0, WHITE);
// Draw slider
Display.fillRect(16, 2, r, 4, WHITE);
//Chan2
// Draw Box around slider
Display.drawRoundRect(14, 18, 44, 8, 0, WHITE);
// Draw slider
Display.fillRect(16, 20, r1, 4, WHITE);
//Chan3
// Draw Box around slider
Display.drawRoundRect(83, 0, 44, 8, 0, WHITE);
// Draw slider
Display.fillRect(85, 2, r2, 4, WHITE);
//Chan4
// Draw Box around slider
Display.drawRoundRect(83, 18, 44, 8, 0, WHITE);
// Draw slider
Display.fillRect(85, 20, r3, 4, WHITE);
//----------
Display.display();
Display.clearDisplay(); //important
}
// LED
//void FillLEDsToRGBColor(CRGB color) {
// for (int i = 0; i < NUM_LEDS; ++i) {
// leds[i] = color;
// }
//}
// LED
//Vanilla DEEJ
void updateSliderValues() {
for (int i = 0; i < NUM_KNOBS; i++) {
analogSliderValues[i] = analogRead(analogInputs[i]);
}
}
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < NUM_KNOBS; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < NUM_KNOBS - 1) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void printSliderValues() {
for (int i = 0; i < NUM_KNOBS; i++) {
String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
Serial.write(printedString.c_str());
if (i < NUM_KNOBS - 1) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
String Format(double val, int dec, int dig ) {
// this is my simple way of formatting a number
// data = Format(number, digits, decimals) when needed
int addpad = 0;
char sbuf[20];
String fdata = (dtostrf(val, dec, dig, sbuf));
int slen = fdata.length();
for ( addpad = 1; addpad <= dec + dig - slen; addpad++) {
fdata = " " + fdata;
}
return (fdata);
}