The program does not work properly

Trying to build a light thing that is controlled by arduino IOT but the problem I am facing is that the when I start the program it does not turn off and the arduino does not change it's power status to zero when I switch of the code with the dashboard all the other function are properly working but the program is not I am pasting the code below please help me out .

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/7ea8a47f-ce70-466d-bdb8-b32f46c92fb9 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int color_state;
  int led_state;
  int program_state;
  bool power;
  bool powerstatus;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
bool red_color;
bool blue_color;
bool green_color;
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  pinMode(0,OUTPUT);
  for (int i = 2 ; i <14 ; i++){
    pinMode(i,OUTPUT);
  }

  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  
}




/*
  Since Led2 is READ_WRITE variable, onLed2Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onLed2Change()  {
  // Add your code here to act upon Led2 change
}

/*
  Since Led3 is READ_WRITE variable, onLed3Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onLed3Change()  {
  // Add your code here to act upon Led3 change
}

/*
  Since Power is READ_WRITE variable, onPowerChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPowerChange()  {
  // Add your code here to act upon Power change
  if (power == 1){
  powerstatus=1;
  digitalWrite(6,HIGH);
  delay(200);
  digitalWrite(7,HIGH);
  delay(200);
  digitalWrite(8,HIGH);
  delay(200);
  digitalWrite(9,HIGH);
  delay(200);
  digitalWrite(6,LOW);
  delay(200);
  digitalWrite(7,LOW);
    delay(200);
  digitalWrite(8,LOW);
    delay(200);
  digitalWrite(9,LOW);
}
  else if (power == 0){
    powerstatus = 0;
  digitalWrite(0,HIGH);
  delay(200);
  digitalWrite(3,HIGH);
  delay(200);
  digitalWrite(4,HIGH);
  delay(200);
  digitalWrite(5,HIGH);
  delay(200);
  digitalWrite(0,LOW);
  delay(200);
  digitalWrite(3,LOW);
    delay(200);
  digitalWrite(4,LOW);
    delay(200);
  digitalWrite(5,LOW);
}
   switch(power){
    case(0):{
      digitalWrite(0,LOW);
      digitalWrite(6,LOW);
      digitalWrite(10,LOW);
      for (int i = 2 ; i <14 ; i++){
        digitalWrite(i,LOW);
      }
      red_color = 0;
      blue_color = 0;
      green_color = 0;
}
   }
}

/*
  Since Powerstatus is READ_WRITE variable, onPowerstatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPowerstatusChange()  {
  // Add your code here to act upon Powerstatus change
}






/*
  Since ColorState is READ_WRITE variable, onColorStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onColorStateChange()  {
  // Add your code here to act upon ColorState change
  if (color_state == 4 && powerstatus == 1){
  red_color =1;
    blue_color = 0;
    green_color = 0;
}
  else if (color_state == 4 && powerstatus == 0){
    digitalWrite(0,LOW);
    red_color = 0;
    blue_color = 0;
    green_color = 0;
  }
  if(color_state == 6 && powerstatus == 1){
    blue_color = 1;
    red_color = 0;
      green_color = 0;
  }
  else if (color_state == 6 && powerstatus == 0){
    digitalWrite(10,LOW);
    blue_color = 0;
  }
  if (color_state == 7 && powerstatus == 1){
    green_color = 1;
    blue_color = 0;
    red_color= 0;
  }
  else if (color_state == 7 && powerstatus == 0){
    digitalWrite(6,LOW);
      green_color = 0;
    blue_color = 0;
    red_color = 0;
  }
  switch(color_state){
    case(8):{
      digitalWrite(0,LOW);
      digitalWrite(6,LOW);
      digitalWrite(10,LOW);
      red_color = 0;
      blue_color = 0;
      green_color = 0;
    }
  }
}




/*
  Since LedState is READ_WRITE variable, onLedStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedStateChange()  {
  // Add your code here to act upon LedState change
  if (led_state == 1 && powerstatus == 1 && red_color == 1){
    digitalWrite(0,HIGH);
     blue_color = 0;
    green_color = 0 ;
  }
  else if (led_state == 2 && powerstatus == 1 && red_color == 1){
    digitalWrite(0,LOW);
    digitalWrite(3,HIGH);
     blue_color = 0;
    green_color = 0 ;
  }
  if (led_state == 3 && powerstatus == 1 && red_color == 1 ){
    digitalWrite(4,HIGH);
    digitalWrite(3,LOW);
     blue_color = 0;
    green_color = 0 ;
  }
  else if (led_state == 4 && powerstatus == 1 && red_color == 1){
    digitalWrite(5,HIGH);
    digitalWrite(4,LOW);
      blue_color = 0;
    green_color = 0 ;
  }
  if (led_state == 1 && powerstatus == 1 && blue_color == 1){
    digitalWrite(10,HIGH);
    red_color = 0;
    green_color = 0;
  }
  else if (led_state == 2 && powerstatus == 1 && blue_color == 1){
    digitalWrite(10,LOW);
    digitalWrite(11,HIGH);
    red_color = 0;
    green_color = 0;
  }
  if (led_state == 3 && powerstatus == 1 && blue_color == 1 ){
    digitalWrite(12,HIGH);
    digitalWrite(11,LOW);
    red_color = 0;
    green_color = 0;
  }
  else if (led_state == 4 && powerstatus == 1 && blue_color == 1){
    digitalWrite(13,HIGH);
    digitalWrite(12,LOW);
    red_color = 0;
    green_color = 0;
  }
  if (led_state == 1 && powerstatus == 1 && green_color == 1){
    digitalWrite(6,HIGH);
    red_color = 0;
    blue_color = 0;
  }
  else if (led_state == 2 && powerstatus == 1 && green_color == 1){
    digitalWrite(6,LOW);
    digitalWrite(7,HIGH);
    red_color = 0;
    blue_color = 0;
  }
  if (led_state == 3 && powerstatus == 1 && green_color == 1 ){
    digitalWrite(8,HIGH);
    digitalWrite(7,LOW);
    red_color = 0;
    blue_color = 0;
  }
  else if (led_state == 4 && powerstatus == 1 && green_color == 1){
    digitalWrite(9,HIGH);
    digitalWrite(8,LOW);
    red_color = 0;
    blue_color = 0;
  }
  if (led_state == 12 && powerstatus == 1 && red_color == 1){
    digitalWrite(0,HIGH);
    digitalWrite(3,HIGH);
     blue_color = 0;
    green_color = 0 ;
  }
  if (led_state == 34 && powerstatus == 1 && red_color == 1){
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
     blue_color = 0;
    green_color = 0 ;
  }
  if (led_state == 8 && powerstatus == 1 && red_color == 1){
    digitalWrite(0,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
     blue_color = 0;
    green_color = 0 ;
  }
  if (led_state == 12 && powerstatus == 1 && blue_color == 1){
    digitalWrite(10,HIGH);
    digitalWrite(11,HIGH);
     red_color = 0;
    green_color = 0 ;
  }
  if (led_state == 34 && powerstatus == 1 && blue_color == 1){
    digitalWrite(12,HIGH);
    digitalWrite(13,HIGH);
     red_color = 0;
    green_color = 0 ;
  }
  if (led_state == 8 && powerstatus == 1 && blue_color == 1){
    digitalWrite(10,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(13,HIGH);
     red_color = 0;
    green_color = 0 ;
  }
   if (led_state == 12 && powerstatus == 1 && green_color == 1){
    digitalWrite(6,HIGH);
    digitalWrite(7,HIGH);
     red_color = 0;
    blue_color = 0 ;
  }
  if (led_state == 34 && powerstatus == 1 && green_color == 1){
    digitalWrite(8,HIGH);
    digitalWrite(9,HIGH);
     red_color = 0;
    blue_color = 0 ;
  }
  if (led_state == 8 && powerstatus == 1 && green_color == 1){
    digitalWrite(6,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    digitalWrite(9,HIGH);
     red_color = 0;
    blue_color = 0 ;
  }
  if (led_state==0 && powerstatus == 1){
    for (int i =2 ; i < 14 ; i++){
      digitalWrite(i,LOW);
      digitalWrite(0,LOW);
    }
  }
  else if (led_state == 1 && powerstatus == 0){
    for(int i = 2; i < 14 ; i++){
      digitalWrite(i,LOW);
      digitalWrite(0,LOW);
    }
  }
}

/*
  Since ProgramState is READ_WRITE variable, onProgramStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onProgramStateChange()  {
  // Add your code here to act upon ProgramState change
  // Your code here 
 do{
   for (int fadevalue = 0; fadevalue < 255; fadevalue += 5){
      analogWrite(0,fadevalue);
      analogWrite(3,fadevalue);
      analogWrite(4,fadevalue);
      analogWrite(5,fadevalue);
      delay(30);
    }
    for (int fadevalue = 255; fadevalue >= 0; fadevalue -= 5){
      analogWrite(0,fadevalue);
      analogWrite(3,fadevalue);
      analogWrite(4,fadevalue);
      analogWrite(5,fadevalue);
      delay(30);
    }
    }while (program_state == 1 && powerstatus == 1);
if (program_state == 1 && powerstatus == 0){
  digitalWrite(0,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
}
  if (program_state != 1 && powerstatus == 1){
    digitalWrite(0,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
      
  }
 do {
   digitalWrite(0,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
 }
   while(program_state == 0 && powerstatus == 1 ); 
}

I have not read the code in details but something that strikes me is your use of long delays and do while loops inside onPowerChange and onProgramStateChange, which likely prevent ArduinoCloud.update() from running often enough.

I look at the thread title and note that the Posting Read Me’s say about thread titles. What might be clickbait on soc media is here like warning members “don’t waste your time”.

Maybe change the title?

I’m out. I don’t want to guess about that code.

1 Like

Nor sure I get what you say. the title is " The program does not work properly "???

1 Like