OLED display "crashing" because of "digitalRead();

Hi!

I am making a remote for my drone, and I am trying to implement a OLED display for my remote but I am having issues fusing it into my project.

I made a small little program with some simple lines of code (as few lines as possible) and got it working well, everything worked as planned.

Then I made my remote witch was working perfecly, but I made a copy of it and experimented with it to make a big fuse witch usually doesnt work perfecly in the beginining. And as excpected the program crashed somewhere so I debugged the code and realised that it crashes when I have theese lines of code in my loop()

  dpdtStatus1_1 = digitalRead(6);
  dpdtStatus1_2 = digitalRead(7);
  dpdtStatus2_1 = digitalRead(8);
  dpdtStatus2_2 = digitalRead(9);
  dpdtStatus3_1 = digitalRead(10);
  dpdtStatus3_2 = digitalRead(11);
  dpdtStatus4_1 = digitalRead(12);
  dpdtStatus4_2 = digitalRead(13);

If I comment out theese lines eveything works well, but with theese lines the program seems to crash in the main() function at this line: display.println("DRONE"); I have also tested each line and I can still use digitalRead pin 6 and 13 without any issue.

My wiring isnt really worth a schimatic but its the default I2C connection to an Arduino Uno, but it is planned to be run on a Arduino Mega.

Here is my OLED test code witch I am trying to fuse:



#include <Wire.h>

#include <Adafruit_SSD1306.h>

#include <Adafruit_GFX.h>

#define OLED_WIDTH 128

#define OLED_HEIGHT 64

#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

void setup() {

  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);

  display.clearDisplay();

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.setCursor(0, 0);

  display.println("MY");

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.setCursor(0, 17);

  display.println("DRONE");

  display.display();

  delay(5000);

}

void loop() {

  display.clearDisplay();

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.setCursor(0, 0);

  display.println("DEBUG: ");

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(0, 17);

  display.println("Throttle: 1000");

  display.println("Yaw: 15");

  display.println("Pitch: 0");

  display.println("Roll : 0");

  display.println("Flightmode: 1");

  display.display();

  delay(5000);

}

And here is the entire code for my remote with both remote and OLED fused:

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

#include <printf.h>

#include <Wire.h>

#include <Adafruit_SSD1306.h>

#include <Adafruit_GFX.h>

#define OLED_WIDTH 128

#define OLED_HEIGHT 64

#define OLED_ADDR   0x3C

#define CE_PIN   18

#define CSN_PIN 19

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

// Create Radio and define pins

RF24 radio(CE_PIN, CSN_PIN);

// Create Radio channels

const uint64_t pAddress = 0xB00B1E5000LL;

//Radio recived array

float dataArray[8];

float feedbackArray[5];

float previousTime;

// PIN VARIABLES

int pinStatus1, pinStatus2, pinStatus3, pinStatus4; // PRESS BUTTONS

int buttonState1, buttonState2, buttonState3, buttonState4;

int dpdtStatus1_1, dpdtStatus1_2, dpdtStatus2_1, dpdtStatus2_2, dpdtStatus3_1, dpdtStatus3_2, dpdtStatus4_1, dpdtStatus4_2; // DPDT SWITCHES, 2 VARIABLES EACH

int dpdt1, dpdt2, dpdt3, dpdt4; 

int potentStatus1; // POTENTIOMETERS

float throttle, yaw, pitch, pitchOffset, roll, rollOffset;

float recivedYaw, recivedPitch, recivedRoll;

int lastButtonState1 = LOW, lastButtonState2 = LOW, lastButtonState3 = LOW, lastButtonState4 = LOW;

unsigned long lastDebounceTime1 = 0, lastDebounceTime2 = 0, lastDebounceTime3 = 0, lastDebounceTime4 = 0;

unsigned long debounceDelay = 50;

void setup() {

  Serial.begin(115200);

  while (!Serial) {}

  printf_begin();

  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);

 

  display.clearDisplay();

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.setCursor(0, 0);

  display.println("MY");

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.setCursor(0, 17);

  display.println("DRONE");

  

  display.display();

  delay(5000);

  // PRESS BUTTONS

  pinMode(2, INPUT);

  pinMode(3, INPUT);

  pinMode(4, INPUT);

  pinMode(5, INPUT);

  // DPDT SIWTCHES

  pinMode(6, INPUT_PULLUP);

  pinMode(7, INPUT_PULLUP);

  pinMode(8, INPUT_PULLUP);

  pinMode(9, INPUT_PULLUP);

  pinMode(10, INPUT_PULLUP);

  pinMode(11, INPUT_PULLUP);

  pinMode(12, INPUT_PULLUP);

  pinMode(13, INPUT_PULLUP);

  // THROTTLE STICKS

  pinMode(14, INPUT);

  pinMode(15, INPUT);

  pinMode(16, INPUT);

  pinMode(17, INPUT);

  if (!radio.begin()) {

    display.clearDisplay();

    display.setTextSize(2);

    display.setTextColor(WHITE);

    display.setCursor(0, 0);

    display.println("ERROR: ");

    display.setTextSize(1);

    display.setTextColor(WHITE);

    display.setCursor(0, 17);

    display.println("Radio not connected.");

    display.display();

    delay(5000);

    while (1) {}

  }

  radio.setPALevel(RF24_PA_MIN);

  radio.setRetries(1, 15);

  radio.openWritingPipe(pAddress);

  radio.printDetails();

  radio.stopListening();

}

void loop() {

  previousTime = millis();

  radio.startListening();

  if (radio.available()) {

    radio.read( &feedbackArray, sizeof(feedbackArray) );

    //Serial.println("Recieved array:");

    //for (byte i = 0; i < 4; i++) {

    //  Serial.println(recivedDataArray[i]);

    //}

    //Serial.println();

    recivedYaw = feedbackArray[0];

    recivedPitch = feedbackArray[1];

    recivedRoll = feedbackArray[2];

  }

  pinStatus1 = digitalRead(2);

  pinStatus2 = digitalRead(3);

  pinStatus3 = digitalRead(4);

  pinStatus4 = digitalRead(5);

/*

  dpdtStatus1_1 = digitalRead(6);

  dpdtStatus1_2 = digitalRead(7);

  dpdtStatus2_1 = digitalRead(8);

  dpdtStatus2_2 = digitalRead(9);

  dpdtStatus3_1 = digitalRead(10);

  dpdtStatus3_2 = digitalRead(11);

  dpdtStatus4_1 = digitalRead(12);

  dpdtStatus4_2 = digitalRead(13);

  */

  potentStatus1 = analogRead(0);

  throttle = analogRead(1);

  yaw = analogRead(2);

  pitch = analogRead(3);

  //roll = analogRead(4);

  /* PRESS BUTTONS DEBOUNCE */

  if (pinStatus1 != lastButtonState1) {

    lastDebounceTime1 = millis();

  }

  if (pinStatus2 != lastButtonState2) {

    lastDebounceTime2 = millis();

  }

  if (pinStatus3 != lastButtonState3) {

    lastDebounceTime3 = millis();

  }

  if (pinStatus4 != lastButtonState4) {

    lastDebounceTime4 = millis();

  }

  if ((millis() - lastDebounceTime1) > debounceDelay) {

    if (pinStatus1 != buttonState1) {

      buttonState1 = pinStatus1;

      if (buttonState1 == HIGH) {

        Serial.println("KNAPP 1 ÄR TRYCKT");

        pitchOffset = pitchOffset - 0.5;

      }

    }

  }

  if ((millis() - lastDebounceTime2) > debounceDelay) {

    if (pinStatus2 != buttonState2) {

      buttonState2 = pinStatus2;

      if (buttonState2 == HIGH) {

        Serial.println("KNAPP 2 ÄR TRYCKT");

        rollOffset = rollOffset + 0.5;

      }

    }

  }

  if ((millis() - lastDebounceTime3) > debounceDelay) {

    if (pinStatus3 != buttonState3) {

      buttonState3 = pinStatus1;

      if (buttonState3 == HIGH) {

        Serial.println("KNAPP 3 ÄR TRYCKT");

        pitchOffset = pitchOffset + 0.5;

      }

    }

  }

  if ((millis() - lastDebounceTime4) > debounceDelay) {

    if (pinStatus4 != buttonState4) {

      buttonState4 = pinStatus1;

      if (buttonState4 == HIGH) {

        Serial.println("KNAPP 4 ÄR TRYCKT");

        rollOffset = rollOffset - 0.5;

      }

    }

  }

  lastButtonState1 = pinStatus1;

  lastButtonState2 = pinStatus2;

  lastButtonState3 = pinStatus3;

  lastButtonState4 = pinStatus4;

  /* END OF PRESS BUTTONS */

  /* DPDT SWITCHES */

  // LEVER 1

  if(dpdtStatus1_1 == LOW && dpdtStatus1_2 == HIGH) {

    Serial.println("LEVER1 MODE 1 (down)");

    dpdt1 = 1;

  }

  if(dpdtStatus1_1 == HIGH && dpdtStatus1_2 == HIGH) {

    Serial.println("LEVER1 MODE 2 (middle)");

    dpdt1 = 2;

  }

  if(dpdtStatus1_1 == HIGH && dpdtStatus1_2 == LOW) {

    Serial.println("LEVER1 MODE 3 (up)");

    dpdt1 = 3;

  }

  // LEVER 2

  if(dpdtStatus2_1 == LOW && dpdtStatus2_2 == HIGH) {

    Serial.println("LEVER2 MODE 1 (down)");

    dpdt2 = 1;

  }

  if(dpdtStatus2_1 == HIGH && dpdtStatus2_2 == HIGH) {

    Serial.println("LEVER2 MODE 2 (middle)");

    dpdt2 = 2;

  }

  if(dpdtStatus2_1 == HIGH && dpdtStatus2_2 == LOW) {

    Serial.println("LEVER2 MODE 3 (up)");

    dpdt2 = 3;

  }

  // LEVER 3

  if(dpdtStatus3_1 == LOW && dpdtStatus3_2 == HIGH) {

    Serial.println("LEVER3 MODE 1 (down)");

    dpdt3 = 1;

  }

  if(dpdtStatus3_1 == HIGH && dpdtStatus3_2 == HIGH) {

    Serial.println("LEVER3 MODE 2 (middle)");

    dpdt3 = 2;

  }

  if(dpdtStatus3_1 == HIGH && dpdtStatus3_2 == LOW) {

    Serial.println("LEVER3 MODE 3 (up)");

    dpdt3 = 3;

  }

  // LEVER 4

  if(dpdtStatus4_1 == LOW && dpdtStatus4_2 == HIGH) {

    Serial.println("LEVER4 MODE 1 (down)");

    dpdt4 = 1;

  }

  if(dpdtStatus4_1 == HIGH && dpdtStatus4_2 == HIGH) {

    Serial.println("LEVER4 MODE 2 (middle)");

    dpdt4 = 2;

  }

  if(dpdtStatus4_1 == HIGH && dpdtStatus4_2 == LOW) {

    Serial.println("LEVER4 MODE 3 (up)");

    dpdt4 = 3;

  }

  

  /* END OF DPDT SWITCHES */

  /* CREATE/PREPARE TRANSMITT ARRAY */

  dataArray[0] = map(throttle, 520, 1023, 1000, 2000);

  if (dataArray[0] < 1000) {

    dataArray[0] = 1000;

  }

  dataArray[1] = map(potentStatus1, 0, 1023, 0, 360) + yaw;

  dataArray[2] = map(roll, 0, 1023, -35, 35) + rollOffset;

  dataArray[3] = map(pitch, 0, 1023, -35, 35) + pitchOffset;

  dataArray[4] = dpdt1;

  dataArray[5] = dpdt2;

  dataArray[6] = dpdt3;

  dataArray[7] = dpdt4;

  /* SEND ARRAY */

  radio.stopListening();

  radio.write( &dataArray, sizeof(dataArray));

  if(millis() - previousTime > 5000) {

    display.clearDisplay();

    display.setTextSize(2);

    display.setTextColor(WHITE);

    display.setCursor(0, 0);

    display.println("DEBUG: ");

    display.setTextSize(1);

    display.setTextColor(WHITE);

    display.setCursor(0, 17);

    display.print("Throttle: ");

    display.println(throttle);

    display.println("Yaw: ");

    display.println(recivedYaw);

    display.print("Pitch: ");

    display.println(recivedPitch);

    display.print("Roll : ");

    display.println(recivedRoll);

    display.println("Flightmode: 1");

    display.display();

  }

  delay(50);

}

*UPDATE *
The NRF24 or anything isnt connected, the only thing that is wired is the OLED display. Can that be a problem? I just want to see if everything runs.

Thanks in advance!
Best regards Max

Aren't you using these for SPI?

Yes I am, Ill change them thank you :slight_smile: . But I cannot have 2,3,4,5 either without the program crashing...

What about A0..A3?

They can remain and it still works.

Consider reading up about arrays.

Do you mean reading all the "pins" into one array?

It works! Thank you!

I also have a problem with this function:

void refreshDebugMonitor() {

  if(millis() - previousTime > 5000) {

    display.clearDisplay();

    display.setTextSize(2);

    display.setTextColor(WHITE);

    display.setCursor(0, 0);

    display.println("DEBUG: ");

    display.setTextSize(1);

    display.setTextColor(WHITE);

    display.setCursor(0, 17);

    display.print("Throttle: ");

    display.println(throttle);

    display.println("Yaw: ");

    display.println(recivedYaw);

    display.print("Pitch: ");

    display.println(recivedPitch);

    display.print("Roll : ");

    display.println(recivedRoll);

    display.println("Flightmode: 1");

    display.display();

  }

}

If I execute this function the program behaves the same, any idea? :slight_smile:

Comment-out the code line by line until it starts working.
Start at the last command.

Not exactly, I meant creating an array of pins… much simpler tom work with, less code.

I made an array pinValues and then added the digitalRead foreach pin I am reading into that array. It worked perfect!

We can't see your code

I did this:


int pinValues[8];

// INSIDE THE LOOP:
pinValues[0] = digitalRead(6);

  pinValues[1] = digitalRead(7);

  pinValues[2] = digitalRead(8);

  pinValues[3] = digitalRead(9);

  pinValues[4] = digitalRead(10);

  pinValues[5] = digitalRead(11);

  pinValues[6] = digitalRead(12);

  pinValues[7] = digitalRead(13);

I commented out every line one by one inside the refreshDebugMonitor() and it came out that it was this line: display.println("Flightmode: 1"); that ruined it, now everything works seemless. Thanks everyone!

Put the pin numbers in another, constant, array, and use a for loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.