Hey everyone,
I am working on a pc temperature/RAM monitor which i wan’t to display on a OLED screen. A program i wrote in c# sends data to the arduino via the serial port. That’s all working well but now i want to include a graph on the OLED screen. this graph has to move to the left as more data is being added ( just like the graphs in task manager). I have to use a circular array for this to keep adding data to the end of the array en remove data from the beginning so that my values shift to the left every time. Now the problem is that whenever I add an array the OLED just stops working.
void Shift(int val){ //Array1 is the array of which i want to shift values
int array2[63]; //This is a temporary array
for(int i = 0; i < 62; i++){ //Move everything from array1 to array2 but shift every value to the left.
array2[i] = array1[i+1];
}
for(int i = 0; i < 62; i++){ //Copy the array back to array1
array1[i] = array2[i];
}
array1[62] = val; //add the new value to the array.
}
This is the function i wrote to shift the data in the array. Now whenever I call this function, even if i call it in the setup for example, the oled doesnt update anything anymore. Even when i just call the function and I dont even use the array.
The code is still running, for example if i write:
Serial.print("A");
Shift(3);
Serial.print("B");
I see AB in the serial monitor so the code still continues but the OLED becomes completely useless.
I tried this on a Arduino UNO and NANO and they both show the same problem.
Here is my complete code if neccessary:
#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
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char character;
String data = "";
String indata = "";
int val = 0;
int array1[63];
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.invertDisplay(true);
delay(500);
display.invertDisplay(false);
display.drawLine(0,0,0,50,WHITE);
display.drawLine(0,50,64,50,WHITE);
display.drawLine(64,0,64,50,WHITE);
display.drawLine(64,50,128,50,WHITE);
display.drawLine(127,50,127,0,WHITE);
display.setTextColor(WHITE, BLACK); // Draw white text
display.setTextSize(1);
display.cp437(true); // Normal 1:1 pixel scale
display.display();
}
void loop() {
while(Serial.available() > 0){
character = Serial.read();
indata += character;
}
if(indata.indexOf("&") != -1){
clearinfo(false);
display.setCursor(1,54);
display.print("CPU: " + extractval(indata, "0", "*") + char(248) + "C");
val = 100 - extractval(indata, "0", "*").toInt();
val = map(val, 0, 100, 0, 50);
display.setCursor(67,54);
display.print("GPU: " + extractval(indata, "*", "#") + char(248) + "C");
Serial.print(extractval(indata, "*", "#"));
display.display();
indata = "";
}
}
String extractval(String input, String c1, String c2){
if(c1 != "0"){
input.remove(0,input.indexOf(c1)+1);
}
input.remove(input.indexOf(c2));
input.trim();
return input;
}
void clearinfo(bool Update){
display.fillRect(0, 51, 128, 64, BLACK);
display.fillRect(1, 0, 63, 50, BLACK);
if(Update){
display.display();
}
}
void Shift(int val){
int array2[63];
for(int i = 0; i < 62; i++){
array2[i] = array1[i+1];
}
for(int i = 0; i < 62; i++){
array1[i] = array2[i];
}
array1[62] = val;
}
I have no idea why this is happening and how to solve it… Hope you guys can help,
Gustav