Hi guys,
I am trying to make button writer on OLED with two buttons one for changing characters and second one for moving one spot to the right. Everything is working fine but after uploading it to arduino it's crashing after maybe 10/20 sec, even without touching it. Earlier it was freezing the screen, now after I've tried to fix it is just displaying black screen.
EDIT: When i ran it after a while it started working fine (i didnt make any changes to the code, just unplugged it for like an hour) But i still want to know what could be the problem with this crashing
#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 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buttonPin1 = 2; // Charachter change button
const int buttonPin2 = 3; // Move button
int charindex = 65;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int cx = 0;
int cy = 0;
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.setTextColor(WHITE);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
boolean color = 1;
display.drawFastHLine(cx, cy+16, 12,color);
display.display();
color=!color;
display.drawFastHLine(cx, cy+16, 12,color);
display.display();
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == LOW) {
delay(50);
// change char
display.fillRect(cx,cy,12,16,BLACK);
display.print(char(charindex));
display.setCursor(cx, cy);
charindex++;
display.display();
if(charindex >80){
charindex = 65; //back to A
}
}
//Move to next field
if (buttonState2 == LOW) {
delay(50);
charindex = 65;
cx=cx+12;
if(cx == 120){
cx = 0;
}
display.setCursor(cx, cy);
}
delay(100);
}