'else' without a previous 'if' error

I've never had this happen before.

#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include "sbus.h"
#include <Embedded_Template_Library.h>


int frameCount = 0;
int holdCount = 0;

#define I2C_ADDRESS 0x3C
#define RST_PIN -1

SSD1306AsciiAvrI2c oled;

SbusRx sbus_rx(&Serial);
SbusTx sbus_tx(&Serial);

void setup() {
  Serial.begin(115200);
  while (!Serial) {}
  
  sbus_rx.Begin();
  sbus_tx.Begin(); // starting sbus communication
  
  oled.begin(&Adafruit128x32, I2C_ADDRESS);
  oled.setFont(Adafruit5x7);
  oled.clear();
  oled.print("   DKVP Systems FLC");
  delay(4000);
  oled.clear();
}
void loop() {
  if (sbus_rx.Read()) {
    for (int i = 0; i < sbus_rx.rx_channels().size(); i++) {
      Serial.print(sbus_rx.rx_channels()[i]);
      Serial.print("\t");
    }
  }
  
    while (sbus_rx.failsafe()) {
      holdCount = holdCount + 1;
      }
    oled.print("Failsafe info: ");
    Serial.print(sbus_rx.failsafe());
    oled.println(sbus_rx.failsafe());
  
    oled.print("Lost Frames: ");
    oled.println(sbus_rx.lost_frame());
    oled.print("\t");
    Serial.println("Lost Frames: ");
    Serial.println(sbus_rx.lost_frame());
  
    oled.print("Holds: ");
    oled.println(holdCount);
    Serial.print("Holds: ");
    Serial.println(holdCount);

  
 else {
   Serial.print("connect SBUS");
   oled.print("connect");
   oled.println("SBUS");
}
  
  }

I also looked at this link: else - Arduino Reference

followed it exactly the same, so I don't know where I am messing up.

Your else is after your while and a load of other code, not your if, which is before the while.

Okay, I'll switch it up and try.

EDIT: that fixed it! Thanks so much @PerryBebbington

1 Like