// C++ code
//
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
int red = 11;
int green = 10;
int blue = 9;
int CLK = 2;
int DT = 3;
int SW = 4;
int Rotator;
int lastStateCLK;
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int aLastState;
#include <U8glib.h>
U8GLIB_SH1106_128X64 My_u8g_Panel(U8G_I2C_OPT_NONE); // I2C / TWI
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
My_u8g_Panel.setFont(u8g_font_unifont);
My_u8g_Panel.drawStr( 0, 10, "WELCOME TO");
My_u8g_Panel.drawStr( 0, 30, "THE COLOR!");
My_u8g_Panel.drawStr( 0, 50, "GAME");
}
void setup(void)
{
int red = 11;
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
// flip screen, if required
// My_u8g_Panel.setRot180();
// assign default color value
My_u8g_Panel.setColorIndex(1); // pixel on
// pinMode(8, OUTPUT);
}
void RGB_color(int red_value, int green_value, int blue_value)
{
// custom function to set three PWM color channels to any given mixture
// it returns NO numerical result (void)
analogWrite(red, red_value);
analogWrite(green, green_value);
analogWrite(blue, blue_value);
}
void loop()
{
// picture loop
My_u8g_Panel.firstPage();
do {
draw();
} while( My_u8g_Panel.nextPage() );
aState = digitalRead(outputA);
{
if (aState != aLastState){
if (digitalRead(outputB) != aState)
{
counter ++;
}
else
{
counter --;
}
}
int led = counter*15;
Serial.print("Position: ");
Serial.println(led);
int redcolor = led*-20;
int bluecolor = led*0.25;
int greencolor = led*7.5;
RGB_color(redcolor, greencolor, bluecolor);
aLastState = aState; // Updates the previous state of the outputA with the current state
}
}