Help check the code for errors!

I have a problem. I wanted to build an OS for MKR IoT Carrier Rev2 using MKR WiFi 1010. But now I've already written some code: Carrier OS - Hackster.io. I wanted to update this now, and whenever I upload the following code, nothing works anymore. Why is that? I hope for help!

#include <Arduino_MKRIoTCarrier.h>
#include <FastLED.h>
MKRIoTCarrier carrier;

float temperature = 0;
float humidity = 0;
unsigned long lastTempUpdate = 0;
int currentPage = 0;
bool inSubMenu = false;
int subMenuPage = 0;
bool ledState = false;
int brightness = 0;


void printDashboard() {
carrier.display.fillScreen(ST77XX_BLACK);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
carrier.display.getTextBounds("SCHAECHNER", 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print("SCHAECHNER");
}

void printTemperature() {
carrier.display.fillScreen(ST77XX_BLUE);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String tempStr = "Temp: " + String(temperature) + " C";
carrier.display.getTextBounds(tempStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(tempStr);
}

void printHumidity() {
carrier.display.fillScreen(ST77XX_GREEN);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String humidityStr = "Humidity: " + String(humidity) + " %";
carrier.display.getTextBounds(humidityStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(humidityStr);
}

void printWeather() {
carrier.display.fillScreen(ST77XX_YELLOW);
carrier.display.setTextColor(ST77XX_BLACK);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
carrier.display.getTextBounds("WEATHER", 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print("WEATHER");
}

void printTemperatureSubMenu() {
carrier.display.fillScreen(ST77XX_BLUE);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String tempStr = "Temp: " + String(temperature) + " C";
carrier.display.getTextBounds(tempStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(tempStr);
}

void printHumiditySubMenu() {
carrier.display.fillScreen(ST77XX_GREEN);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String humidityStr = "Humidity: " + String(humidity) + " %";
carrier.display.getTextBounds(humidityStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(humidityStr);
}


void printSettings() {
carrier.display.fillScreen(ST77XX_MAGENTA);
carrier.display.setTextColor(ST77XX_BLACK);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
carrier.display.getTextBounds("Settings", 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print("Settings");
}

void printLight() {
  carrier.display.fillScreen(ST77XX_ORANGE);
  carrier.display.setTextColor(ST77XX_BLACK);
  carrier.display.setTextSize(2);
  int16_t x, y;
  uint16_t width, height;
  String text = "LED: ";
  
  // Check if the button 0 is pressed
  if (digitalRead(0) == HIGH) {
    // Increase the brightness by 10% until it reaches 100%
    brightness += 10;
    if (brightness > 100) {
      // Reset the brightness to 0%
      brightness = 0;
      ledState = false;
    } else {
      ledState = true;
    }
  }

  if (ledState) {
    text += "ON";
  } else {
    text += "OFF";
  }
  
  carrier.display.getTextBounds(text, 0, 0, &x, &y, &width, &height);
  
  carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
  
  carrier.display.print(text);
  
  if (ledState) {
    // Set the LED brightness according to the global variable
    carrier.leds.fill(CRGB::White);
    carrier.leds.setBrightness(brightness);
    carrier.leds.show();
    
  } else {
    // Set the LED color to black
    carrier.leds.fill(CRGB::Black);
    carrier.leds.show();
    
  }
}

void setup() {
    // Start sound
    carrier.display.setRotation(0);
  carrier.Buzzer.sound(523);
  delay(100);
  carrier.Buzzer.sound(659);
  delay(100);
  carrier.Buzzer.sound(784);
  delay(100);
  carrier.Buzzer.sound(880);
  delay(100);
  carrier.Buzzer.sound(1047);
  delay(500);
  carrier.Buzzer.noSound();
  CARRIER_CASE = false;
  carrier.begin();

  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();

}

void loop() {

if (carrier.Buttons.onTouchDown(TOUCH0)) {
    if (inSubMenu && currentPage == 2 && subMenuPage == 0) {
      ledState = !ledState;
      printLight();
    }
    else if (currentPage == 2) {
      inSubMenu = true;
      subMenuPage = 0;
      printLight();
    }
}
 
  carrier.Buttons.update();
  unsigned long now = millis();
  
  if(now - lastTempUpdate > 60000){
    temperature = carrier.Env.readTemperature();
    humidity = carrier.Env.readHumidity();
    lastTempUpdate = now;
  }

  if (carrier.Buttons.onTouchDown(TOUCH2)) {
    currentPage = 0;
    inSubMenu = false;
    printDashboard();
  }
  else if (carrier.Buttons.onTouchDown(TOUCH1) || carrier.Buttons.onTouchDown(TOUCH3)) {
    if (!inSubMenu) {
      currentPage = (currentPage + 1) % 4;
      switch(currentPage) {
        case 0:
          printDashboard();
          break;
        case 1:
          printWeather();
          break;
        case 2:
          printSettings();
          break;
      }
    }
    else {
      subMenuPage = (subMenuPage + 1) % 2;
      switch(currentPage) {
        case 1:
          switch(subMenuPage) {
            case 0:
              printTemperatureSubMenu();
              break;
            case 1:
              printHumiditySubMenu();
              break;
          }
          break;
        case 2:
          switch(subMenuPage) {
            case 0:
              printLight();
              break;
            case 1:
              printTemperature();
              break;
          }
          break;
      }
    }
  }
  else if (carrier.Buttons.onTouchDown(TOUCH4)) {
    if ((currentPage == 1 || currentPage ==2) && !inSubMenu) {
      inSubMenu = true;
      subMenuPage = (subMenuPage + 1) %2;
      switch(currentPage) {
        case 1:
          switch(subMenuPage) {
            case 0:
              printTemperatureSubMenu();
              break;
            case 1:
              printHumiditySubMenu();
              break;
          }
          break;
        case 2:
          switch(subMenuPage) {
            case 0:
              printLight();
              break;
            case 1:
              printTemperature();
              break;
          }
          break; 
      }
    }
    else if (inSubMenu) {
      inSubMenu = false;
      switch(currentPage) {
        case 1:
          printWeather();
          break;
        case 2:
          printSettings();
          break; 
      }
    }
}}

Hi @cloudDev

Please provide a detailed explanation of what you mean by this, including:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?
    • Make sure to include the full and exact text of any error or warning message you might have encountered.

I already found the solution how to fix it. The only thing I care about is that the whole code is worthless now.... But thanks for trying to solve the problem!
That was my help:
Reset your board – Arduino Help Center
An error just flashed in Windows. The only thing that helped was double-clicking on the reset button on the board.

1 Like

It would be nice to update the thread title to reflect your solution.

Not necessarily. More likely there is one specific part of the code that is not working as you expected.

It is often helpful to start stripping out all the irrelevant parts of your code until you have the most minimal possible program that still produces the fault. Once you have done that, it is often clear what the problem was. Even if it isn't, you can then include that "minimal, reproducible, verifiable example (MCVE)" in a request for assistance here on the forum. The helpers here will find it very useful.

1 Like

Thank you, I'll try it. Maybe a AI can also help, because with its help I also programmed it.

Check your memory usage. I don't see any possibility of an array overflow, from a quick read of your code. Good luck with the AI, when you ask it about code, what you are getting back is the results of a fancy search engine. So it uses all the bad code as well as the good, because it can't tell the difference.

If you used an AI to write it, it's a faint surprise that it doesn't work. By the way, for that reason, AI solutions to posted questions are banned here.

Thank you for your effort. I usually have enough storage space. I'll check again anyway!

You might not, if you are not looking at dynamically allocated memory. Display drivers are famous for using gobs of that for display buffers, and it doesn't show up in the compile stats.

1 Like

SOLUTION

I found the solution: It was the setup code. I rotated the display first before it played the sound. I don't know why this is a problem, anyway it's solved! :clap:

void setup() {
    // Start sound
  carrier.Buzzer.sound(523);
  delay(200);
  carrier.Buzzer.sound(659);
  delay(200);
  carrier.Buzzer.sound(784);
  delay(200);
  carrier.Buzzer.sound(880);
  delay(200);
  carrier.Buzzer.sound(1047);
  delay(600);
  carrier.Buzzer.noSound();
  CARRIER_CASE = false;
  carrier.begin();
  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();
  carrier.display.setRotation(0);

}

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