A Code for MKR IoT Carrier Rev2 - Please help!

I already wrote a post about the MKR IoT Carrier Rev2 OS: MKR Iot Carrier (Rev2) OS (Operating System), but I still have one Ask. Here is my new code. The LEDs should switch on and off with button 00 when you are in the submenu. That works too. But when I go into the menu, the light is already on. How can I solve this?

Thank you in advance for your answer!

#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 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 button 0 is pressed
  if (digitalRead(0) == HIGH) {
    // Increase brightness by 10% until it reaches 100%
    brightness += 10;
    if (brightness > 100) {
      // Reset brightness to 0%
      brightness = 0;
      ledState = false;
    } else {
      ledState = true;
    }
  } else {
    ledState = false; // Turn off the LED if button 0 is not pressed
  }
  
  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() {
  CARRIER_CASE = false;
  carrier.begin();
  carrier.display.setRotation(0);
  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:
              printTemperature();
              break;
            case 1:
              printHumidity();
              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:
              printTemperature();
              break;
            case 1:
              printHumidity();
              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; 
      }
    }
}}

I started reading your code and then abandoned reading as I saw nested switch-cases combined with hard-coded numbers.

My recommendation is to create a documentation that shows how your menu is structured.

What does your mainmenu have for items?
How many items does your main-menu have?
How many stages of

  • sub
  • or sub-sub
  • or sub-sub-sub-menues do you have?

What does each sub-menu show?

my recommendation is to re-structuring your code to have just 1 stage of nesting

Maybe there are real coding-professionals out there for whom your question is a piece of cake.
Me personal it would take me hours just to understand what the logic of your code is.

Then your text is very unspecific:

which exact menu?

  • do you mean the switch LEDs on/off menu which you say is working? So where is the problem?

the light is already on. Does "light" mean the same as LEDs? or a different light?

What in your code equals to "button 00" ?

carrier.Buttons.onTouchDown(TOUCH0) ??

or is it

// Check if button 0 is pressed
if (digitalRead(0) == HIGH) { ???

First of all, thank you very much for looking at my post.
I can't program like that myself, so an AI helped me with this code. But it is structured like this:

  • Dashboard
  • Weather
  • Settings
    • Temperature (can be exchanged for something else)
    • LED (or light)

By button 00 I mean

carrier.Buttons.onTouchDown(TOUCH0)

just like
if (digitalRead(0) == HIGH) {. The button is labeled 00 on the Carrier Rev2.

The light turns on when I go into the Settings menu and then switch to LED.

Thanks very much,
cloudDev

So I guess your specification of the wanted functionality that you send to the KI was not precise enough.

And now a human beeing shall analyse what a AI wrote for code for you? Because you were unable to specify precise enough what functionality you want ?
.
.
Me ?

NEVER! :angry: :triumph: :rage:

Go asking this question your AI !

@cloudDev ,
As you are well aware from other discussions on the forum the idea of people getting help with AI generated code is proving controversial (I am keeping things polite here), so if you get a bad reaction to it then maybe that's understandable. Maybe @StefanL38 could have expressed himself in a more friendly way, but you already know what people think of AI around here.

He is trying to help you.

Hi @cloudDev ,

If you felt uncomfortable with the answer that I have given I apologise for that.

What I answer here is not the answer to your initial question.
Your project and your question has a very interesting aspect.
And in my humble opinion you can learn something very important from it:

Your project - which is really harmless - demonstrates what can happen if you use artificial intelligence. (AI)

There is a person that has a limited knowledge on a certain field. This person is asking an AI to create a certain functionality (in software)
or a certain product
or a
certain procedure
or
a certain law

The description for the AI might contain a lot of details, but as the person who is commanding the AI has a low knowledge-level about the subject some very important details are missing.

The AI does its best to create the desired functionality based on the given description

The AI is able to create a result. Though the result is unsatisfying. But as the person who commanded the AI does not understand how this result was achieved nor how the result works in detail this person is unable to correct the result!

This person is unable to correct the result!

Now imagine a not enough experienced person commanding an AI to create a surgery robot for cardiac surgery.

Would you entrust yourself to such a robot in case you need a heart-surgery??? I guess not.

Our civilisation highly depends on specialists knowledge and computers. It requires hundreds of different specialists for producing a microcontroller.
pure silicium-chrystal production
silicium-wafer cutting
wafer-polishing
silcium-dotating
applying photosensitives
x-ray focused lithografy
cleanest-room technology

logical designing on the stage of math-co-math-processor etc.
transfering logical designs to physical functional transistors
thermal design making sure there is enough cooling
checking if electrical field-strength does not create destroying sparks at the applied voltage-levels
reliability to make billions of transistors work without a single failure.

and this is only that part that I know of through reading scientific magazines out of interest.

And now more and more of this shall be handed over to AI which eliminates the urgend need for the high-tech knowledge?
Reducing the number of human specialists?

Elon Musk tried to build a fully automated car-production and failed. To get it to work more reliable than a partially automated "classical" car-production.

There are reasons why lately Elon Musk and other entrepreneurs warned to apply more and more AI in complex applications.

best regards Stefan

2 Likes

AI is not very popular - therefore no help.

You should not.

@cloudDev should understand the use of the code as AI fight is already going on
Please go through this

Thank you, I already got it.

But I would still appreciate any help. But please don't scold that the code is from an AI (see above!)! I shouldn't have posted that. Otherwise I might have the solution now...

Ok @cloudDev I understand

But I am not scolding you I appreciate that you are willing to make some project with the help of AI but you should also see from your side that how the code works it is bit important because if we say something for your project or ask some more information you will not able to provide as you are not well aware with that.

Thanks
Krishna

Hello @krishna_agarwal ,
That's just my problem: I think I can at least know how the code works. I still can't find the error.
Best regards,
cloudDev

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