Tyvärr är jag inte kapabel att läsa koden djupt nere i bibliotek men vi kom fram till att u8g.firstPage(); gör en initialisering/reset/clear samt ytterligare något som "inte går att vara utan".
Tog nya tag och bläddrade runt bland OLED bibliotek och hittade Adafruits GFX-bibliotek som tycktes ha lite enklare filer och se på fasen. Efter en stund så fungerar det nu klockrent!
Hade kunnat skicka en tårta till dom som skrivit biblioteket...
Så här ser det ut nu. Har inte städat rent koden än men en liten resultatbild syns nedan också. Inte ett enda blink på skärmen så långt ögat kan nå
Kod: Markera allt
/*****************************************PROJECT INFO*************************************************
* Name: Gorby_brain.c
* Processor: ATmega328P
* Author: Ida & Magnus
* Peripherals: 128x64 I2C OLED Display
* Ultrasonic sensor
* 4 ea pushbuttons
*
*****************************************************************************************************/
/*****************************************INCLUDES****************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*****************************************************************************************************/
/******************************************DEFINES****************************************************/
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
/*****************************************************************************************************/
/*****************************************CONSTANTS****************************************************/
const uint8_t buttonLeft = 9;
const uint8_t buttonRight = 10;
const uint8_t buttonFwd = 11;
const uint8_t buttonX = 12;
const unsigned char arrow_up [] PROGMEM = {
0x00, 0x00, 0x01, 0x00, 0x03, 0x80, 0x07, 0xC0, 0x0F, 0xE0, 0x1F, 0xF0, 0x3F, 0xF8, 0x7B, 0xBC,
0x73, 0x9C, 0x63, 0x8C, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80,
0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char arrow_left [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x07, 0x80, 0x0F, 0x00, 0x1E, 0x00, 0x3F, 0xFE,
0x7F, 0xFE, 0x3F, 0xFE, 0x1E, 0x00, 0x0F, 0x00, 0x07, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char arrow_right [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x01, 0xE0, 0x00, 0xF0, 0x00, 0x78, 0x7F, 0xFC,
0x7F, 0xFE, 0x7F, 0xFC, 0x00, 0x78, 0x00, 0xF0, 0x01, 0xE0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
/*****************************************************************************************************/
/**************************************GLOBAL VARIABLES***********************************************/
uint32_t debounceDelay = 5;
uint32_t lastDebounceChk = 0;
uint32_t lastRefresh = 0;
uint32_t screenRefreshRate = 300;
uint8_t debounceReg_Left = 0xFF;
uint8_t debounceReg_Right = 0xFF;
uint8_t debounceReg_Fwd = 0xFF;
uint8_t debounceReg_X = 0xFF;
uint8_t buttonLeft_Pressed = 0;
uint8_t buttonRight_Pressed = 0;
uint8_t buttonFwd_Pressed = 0;
uint8_t buttonX_Pressed = 0;
uint8_t Left_Press_Done = 0;
uint8_t Right_Press_Done = 0;
uint8_t Fwd_Press_Done = 0;
uint8_t X_Press_Done = 0;
uint8_t buttonCounter = 0;
uint8_t commandCounter = 0;
uint8_t X_axis = 0;
uint8_t Y_axis = 0;
uint8_t commands[24];
enum Directions : uint8_t {NONE, LEFT, RIGHT, FWD, X};
enum Directions key;
enum StatusOfButtons : uint8_t {WAIT_FOR_KEYPRESS, WAIT_FOR_KEYRELEASE};
enum StatusOfButtons Buttonstatus;
/*****************************************************************************************************/
void screenUpdate() {
if(X_axis>=128){
X_axis=0;
Y_axis+=20;
if(Y_axis>=60){
Y_axis=0;
}
}
switch(commands[commandCounter]){
case 'F':
display.drawBitmap(X_axis, Y_axis, arrow_up, 16, 20, 1);
X_axis+=16;
break;
case 'L':
display.drawBitmap(X_axis, Y_axis, arrow_left, 16, 20, 1);
X_axis+=16;
break;
case 'R':
display.drawBitmap(X_axis, Y_axis, arrow_right, 16, 20, 1);
X_axis+=16;
break;
}
display.display();
}
void pushButtonChk() {
if(millis() >= (lastDebounceChk + debounceDelay)) {
lastDebounceChk = millis();
debounceReg_Left <<= 1; // Every time the function is called
debounceReg_Right <<= 1; // the debounce registers is being shifted
debounceReg_Fwd <<= 1; // left and then the current button status
debounceReg_X <<= 1; // is read and added
debounceReg_Left |= digitalRead(buttonLeft);
debounceReg_Right |= digitalRead(buttonRight);
debounceReg_Fwd |= digitalRead(buttonFwd);
debounceReg_X |= digitalRead(buttonX);
if(debounceReg_Left == 0x00) { // As soon as the debounce register is full
key = LEFT; // then the system reads the button as pushed
}
if(debounceReg_Right == 0x00) {
key = RIGHT;
}
if(debounceReg_Fwd == 0x00) {
key = FWD;
}
if(debounceReg_X == 0x00) {
key = X;
}
else if((debounceReg_X & debounceReg_Fwd & debounceReg_Right & debounceReg_Left) == 0xFF) {
key = NONE;
}
}
}
void setup() { // Using internal pullup for buttons
Serial.begin(9600);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
pinMode(buttonFwd, INPUT);
pinMode(buttonX, INPUT);
digitalWrite(buttonLeft, HIGH);
digitalWrite(buttonRight, HIGH);
digitalWrite(buttonFwd, HIGH);
digitalWrite(buttonX, HIGH);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
pushButtonChk();
//screenUpdate();
switch(Buttonstatus) {
case WAIT_FOR_KEYPRESS :
buttonCounter = 0;
switch(key) {
case LEFT :
if(!Left_Press_Done) {
Left_Press_Done = 1;
commands[commandCounter] = 'L';
screenUpdate();
commandCounter++;
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case RIGHT :
if(!Right_Press_Done) {
Right_Press_Done = 1;
commands[commandCounter] = 'R';
screenUpdate();
commandCounter++;
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case FWD :
if(!Fwd_Press_Done) {
Fwd_Press_Done = 1;
commands[commandCounter] = 'F';
screenUpdate();
commandCounter++;
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case X :
if(!X_Press_Done) {
X_Press_Done = 1;
if(commandCounter > 0){
commandCounter--;
}
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case NONE :
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
}
break;
case WAIT_FOR_KEYRELEASE :
switch(key) {
case NONE :
Left_Press_Done = 0;
Right_Press_Done = 0;
Fwd_Press_Done = 0;
X_Press_Done = 0;
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
}
break;
}
}