# Struct array wrong data

**URL:** <https://forum.arduino.cc/t/struct-array-wrong-data/1316145>\
**Category:** Programming\
**Created:** [October 28, 2024, 5:28pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145 "2024-10-28T17:28:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 28, 2024, 5:28pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/1 "2024-10-28T17:28:24Z")

</div>

Hello guys i have problem with my code :

```arduino
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "OS.h"
#include "PubSubClient.h"
#include "mqtt_handler.h"
#include "feed_item.h"
#include <Preferences.h>
#include "ESP32Time.h"

#define INIT_PIN 15

extern Preferences drive;
extern WebServer server;
extern const char *mqtt_server;
extern PubSubClient client;
extern Servo feed_servo;

ESP32Time rtc(3600);
feed_item times[3] = {};

const char *SERIAL_NUMBER = "TWR876";

bool setup_wifi = false;

void IRAM_ATTR rst()
{
  ESP.restart();
}

void setup()
{
  Serial.begin(115200);

  // setup
  pinMode(INIT_PIN, INPUT);

  feed_servo.attach(FEED_SERVO_PIN);

  feed_servo.write(0);

  // init system

  if (digitalRead(INIT_PIN) == LOW)
  {
    unsigned long time = millis();
    while (digitalRead(INIT_PIN) == LOW)
    {
    }
    unsigned long now = millis();
    if (now - time >= 6000)
    {
      clear_times_data();
    }
    else if (now - time >= 4999)
    {
      drive.begin("wifi", false);
      drive.clear();
      drive.end();
      delay(200);
    }
  }

  drive.begin("feed_system", true); // Open namespace for reading
for (int i = 0; i < 3; i++) {
    feed_item it = load_times_data(i);
    Serial.printf("Loaded from load_times_data %d: %02d:%02d:%02d\n", i, it.H, it.M, it.S);
    times[i] = it; // Direct assignment to times[i]
    Serial.printf("Assigned Time for %d: %02d:%02d:%02d\n", i, times[i].H, times[i].M, times[i].S);
}

  drive.end();

  attachInterrupt(digitalPinToInterrupt(15), rst, FALLING);
  // end init system

  // get wifi data
  drive.begin("wifi", false);
  String ssid = drive.getString("ssid", "");
  delay(200);
  String pass = drive.getString("pass", "");
  delay(200);
  drive.end();

  rtc.setTime(30, 24, 14, 17, 1, 2021);

  if (ssid == "")
  {
    setup_wifi = true;
    // wifi setup
    WiFiSetup();
    server.begin();
  }
  else
  {
    // connect wifi
    WiFi.softAPdisconnect(true);
    WiFiConnect(ssid, pass);
  }

  // mqtt setup
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{
  // iot system check

  if (setup_wifi)
  {
    server.handleClient();
  }
  else if (!WiFi.isConnected())
  {
    WiFi.reconnect();
  }
  else if (!client.connected())
  {
    reconnect();
    delay(1000);
    client.publish("fed1", "online");
  }
  else
  {
    client.loop();
  }
  Serial.println(rtc.getTime());
  Serial.printf("%d:%d:%d\n", times[0].H, times[0].M, times[0].S);
}

```

my output is wrong output:

```arduino
Feed item 0 loaded: Time: 13:12:00, Play Sound: 0, Delay: 20, Work: 1
Loaded from load_times_data 0: 13:12:00
Assigned Time for 0: 13:12:00
Feed item 1 loaded: Time: 13:12:00, Play Sound: 0, Delay: 20, Work: 1
Loaded from load_times_data 1: 13:12:00
Assigned Time for 1: 13:12:00
Feed item 2 loaded: Time: 13:12:00, Play Sound: 0, Delay: 20, Work: 1
Loaded from load_times_data 2: 13:12:00
Assigned Time for 2: 13:12:00
..
192.168.100.18
Attempting MQTT connection...connected
15:24:35
620773686:-1306523032:172156192

```

feed Item :

```arduino
struct feed_item {
    int H;
    int M;
    int S;
    bool play_sound;
    int delay;
    bool work;
};

```

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 28, 2024, 5:32pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/2 "2024-10-28T17:32:14Z")

</div>

ok, and what is expected?

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 28, 2024, 6:52pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/3 "2024-10-28T18:52:27Z")

</div>

> [@eskandarijoon](#):
>
> `620773686:-1306523032:172156192`

13:12:00 instead 620773686:-1306523032:172156192

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 28, 2024, 7:22pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/5 "2024-10-28T19:22:17Z")

</div>

> [@eskandarijoon](#):
>
> 620773686

it is definitely not type int.

> [@eskandarijoon](#):
>
> ```arduino
> Serial.println(rtc.getTime());
> Serial.printf("%d:%d:%d\n", times[0].H, times[0].M, times[0].S);
> 
> ```

maybe try first

```arduino
  Serial.println(rtc.getTime());
  Serial.println(times[0].H);
  Serial.println(times[0].M);
  Serial.println(times[0].S);

```

and/or

```arduino
  Serial.println(rtc.getTime());
  byte b = times[0].H
  Serial.println(b);
  b = times[0].M
  Serial.println(b);
  b = times[0].S
  Serial.println(b);

```

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 1:45pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/6 "2024-10-29T13:45:26Z")

</div>

im sorry for miss information i just want when I load 13:12:00 the code show that instead of 620773686:-1306523032:172156192  
its simple code I want to work :

```arduino
struct feed_item {
    int H;
    int M;
    int S;
    bool play_sound;
    int delay_time;
    bool work;
};

feed_item times[3] = {{12,13,14,false,1000,true},{12,15,34,false,3000,true},{18,23,54,false,2000,true}};
void setup() {
  Serial.begin(115200);

    for (int i = 0; i < 3; i++) {
    Serial.print("Item ");
    Serial.print(i);
    Serial.print(": time = ");
    Serial.printf("%d:%d:%d",times[i].H,times[i].M,times[i].S);
    Serial.print(", flag = ");
    Serial.println(times[i].play_sound ? "true" : "false");
  }
}

void loop() {

}

```

my out put is :

```arduino
Item 0: time = 620773686:-1306523032:172156192, flag = true
Item 1: time = -1306523034:549662752:1415906525, flag = true
Item 2: time = -266470525:201343286:15736066, flag = true

```

I want to get :  
`item 0:time = 12:13:14 , ...........`

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 1:56pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/7 "2024-10-29T13:56:00Z")

</div>

> [@kolaha](#):
>
> ```arduino
> byte b = times[0].H
> Serial.println(b);
> 
> ```

i check the byte it was 54 idk why and how to fix that

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 2:03pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/9 "2024-10-29T14:03:39Z")

</div>

this simple code i show you didnt work too

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 29, 2024, 2:04pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/10 "2024-10-29T14:04:07Z")

</div>

> [@eskandarijoon](#):
>
> ```arduino
> struct feed_item {
> int H;
> int M;
> int S;
> bool play_sound;
> int delay_time;
> bool work;
> };
> 
> ```

try

```arduino
struct feed_item {
    int32_t H;
    int32_t M;
    int32_t S;
    int32_t delay_time;
    int32_t BoolFlags;
};
feed_item times[3] = {
 {12,13,14,1000, (false<<1) | (true<<0)},
 {12,15,34,3000, (false<<1) | (true<<0)},
 {18,23,54,2000, (false<<1) | (true<<0)}
};

```

maybe you will need to modify other piece of your code, like this

```arduino
Serial.println(times[i].play_sound ? "true" : "false");
to
Serial.println(times[i].BoolFlags&0x02? "true" : "false");

```

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 2:07pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/11 "2024-10-29T14:07:26Z")

</div>

> [@kolaha](#):
>
> ```arduino
> feed_item times[3] = {
> {12,13,14,1000, (false<<1) | (true<<0)},
> {12,15,34,3000, (false<<1) | (true<<0)},
> {18,23,54,2000, (false<<1) | (true<<0)}
> };
> 
> ```

steel i got :

```arduino
Item 0: time = 620773686:-1306523032:172156192, flag = true
Item 1: time = -452968138:-1306523034:549662752, flag = true
Item 2: time = 240:553664822:-266470525, flag = true

```

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 29, 2024, 2:09pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/12 "2024-10-29T14:09:58Z")

</div>

can you shorting the code that i can test it in simulator?

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 2:10pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/13 "2024-10-29T14:10:40Z")

</div>

yes :

```arduino
struct feed_item {
    int32_t H;
    int32_t M;
    int32_t S;
    int32_t delay_time;
    int32_t BoolFlags;
};
feed_item times[3] = {
 {12,13,14,1000, (false<<1) | (true<<0)},
 {12,15,34,3000, (false<<1) | (true<<0)},
 {18,23,54,2000, (false<<1) | (true<<0)}
};
void setup() {
  Serial.begin(115200);

    for (int i = 0; i < 3; i++) {
    Serial.print("Item ");
    Serial.print(i);
    Serial.print(": time = ");
    Serial.printf("%d:%d:%d",times[i].H,times[i].M,times[i].S);
    Serial.print(", flag = ");
    Serial.println(times[i].BoolFlags ? "true" : "false");
  }
}

void loop() {

}

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 29, 2024, 2:13pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/14 "2024-10-29T14:13:10Z")

</div>

> [@eskandarijoon](#):
>
> `feed_item times[3] = {{12,13,14,false,1000,true},{12,15,34,false,3000,true},{18,23,54,false,2000,true}};`

That may be an unfortunate choice of names. `times` appears to be unusable as an array name (don't know why ... reserved word, function name, who knows). When I changed it to something else, your code worked.

---

<div class="post-metadata">

**Author:** ![eskandarijoon](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eskandarijoon](https://forum.arduino.cc/u/eskandarijoon)\
**Post date:** [October 29, 2024, 2:16pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/15 "2024-10-29T14:16:28Z")

</div>

no way dude thanks a lot you saved me thanks dude  
and thanks other tried to help 😍

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 29, 2024, 2:24pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/16 "2024-10-29T14:24:10Z")

</div>

mark his answer as solution

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2025, 2:24pm UTC](https://forum.arduino.cc/t/struct-array-wrong-data/1316145/17 "2025-04-27T14:24:53Z")

</div>

Closed on April 27, 2025, 2:24pm UTC

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