#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define SWPIN 4
#define NPpin 5
#define NUMPIXELS 24
Adafruit_NeoPixel NPring = Adafruit_NeoPixel(NUMPIXELS, NPpin, NEO_GRB + NEO_KHZ800);
const int MID_AVAL = 2047;
const int MAX_AVAL = 4095;
int avalue = 0; // analog value
float speed = 0; // rotation speed 0 - 100%
bool direction = 0; // direction 0 = CW, 1 = CCW
bool rotate = 0; // 0 = still, 1 = rotate
int pixel_idx = 0; // pixel index
bool finish = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
NPring.begin();
NPring.show(); // initialize all pixels to 'off'
pinMode(SWPIN, INPUT_PULLUP);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
// oled.setCursor(0, 2); // set position to display (x,y)
// oled.println("Robotronix"); // set text
// oled.display(); // display on OLED
}
void loop() {
// if (!finish) clear_pixel(pixel_idx);
if (digitalRead(SWPIN)) {
if (speed !=0) {
speed -= random(3,5);
if (speed < 2.0) {
speed = 0;
finish = 1;
direction = !direction;
oled_display();
}
}
}
else {
clear_pixel(pixel_idx);
speed = 100;
finish = 0;
}
}
void clear_pixel(int i) {
NPring.setPixelColor(i,0,0,0);
NPring.show();
pinMode(SWPIN, INPUT_PULLUP);
}
// void set_color_pixel(int i) {
// int rval = (int)(speed+100);
// int gval = (int)direction*2*(120 - speed);
// int bval = (int)(1-direction)*2*(120 - speed);
// NPring.setPixelColor(i, rval,gval,bval);
// NPring.show();
// }
void oled_display() {
oled.clearDisplay();
oled.setCursor(0,0);
oled.print("Speed : ");
oled.println(speed);
oled.setCursor(0,20);
oled.print("Dir : ");
if (direction) oled.println("CCW");
else oled.println("CW");
oled.display();
}