Hi,
I want to create an empty array of size 3 and then in a for loop add 3 element containg the integers 30000, 60000 and 90000. My code:
int timeStep = 30000;
int timeIntervall[3];
int i = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int t = timeStep;i<=2;t=t+timeStep){
timeIntervall[i] = t;
i++;
}
}
If I print out the values for t in each step in the for loop I get 30 000, 60 000, 90 000, which is correct. But if I print out the value of timeIntervall[i] in each step its completely wrong.
What Arduino are you using? On 8-bit Arduinos, an 'int can't hold more than 32767 and that could be your problem. Since you don't show the code that produces the output that you don't show, it is hard to speculate more.
For values over 30 000, I would use 'unsigned long' variables.
unsigned long timeStep = 30000;
unsigned long timeIntervall[3];
void setup()
{
Serial.begin(9600);
unsigned long t = timeStep;
for (int i = 0; i < 3; i++)
{
timeIntervall[i] = t;
t += timeStep;
}
for (int i = 0; i < 3; i++)
{
Serial.println(timeIntervall[i]);
}
}
void loop()
{
}
So it must be a 32-bit Arduino, and why the correct values are not assigned to the array is a mystery. Please provide more info, like the model of Arduino and the full code that prints the array contents and the output you see on serial monitor (copy text from Serial Monitor and paste into your post between code tags),
1. Even though the values for variables are declared/shown in decimal, they are always stored as "bits" in memory locations. Accordingly, the numbers: 30000, 60000, and 90000 would be stored as: 0x7530 (written in hex base for convenience), 0xEA60, and 0x00015F90 respectively.
Note that 0x15F90 (for 90000) is written as 0x00015F90 (32-bit) due to the fact that in Computer memory, the numbers are stored as 8-bit or 16-bit or 32-bit or 64-bit chunk; where, the required sizes are maintained by appending 0s.
2. To avoid mistakes in the selection of appropriate data type, it is peferable that the hex versions of the decimal numbers are written down on note papers.
3. As the values of Step-1 are to be stored in a 3-elemnt array, the sizes of the array elements should be the same. Accordingly, we can declare the following data type for the array, which can hold number from 0 to 4 294 967 295 (0x00000000 to 0xFFFFFFFF).
unsigned long int timeInterval[3]; //the data type is: "unsigned long int"
4. The int type variable holds data of this range: -32768 to 32767 (0x8000 to 0x7FFF). Obviously, the data value 90000 would not be accomodated in an int type variable.
5. The following sketch stores/prints the data correctly (Fig-1) on Arduino UNO.
int timeStep = 30000;
unsigned long int timeIntervall[3];
int i = 0;
void setup()
{
Serial.begin(9600);
//-------------------
unsigned long int t = timeStep;
for (i = 0; i < 3; i++)
{
timeIntervall[i] = t;
t = t + timeStep;
Serial.println(timeIntervall[i], DEC);
}
}
void loop()
{
}
@GolamMostafa please do not set a bad example to newcomers by posting images of the serial monitor! The text should be copied and pasted between code tags. Serial Plotter output must be posted as an image, of course.
This is not always true. On most types of Arduino, such as Uno, Mega, Nano, which have 8-bit processors, it is true, an int is 16 bits and can hold 30000, maybe 60000 if it is unsigned int, but certainly not 90000. But for more modern types of Arduino such as Due, Zero, Nano 33, ESP8266/32, an int is 32 bits. This must be true for @scorpo because they see 30000, 60000, 90000 printed correctly and their sketch uses int, not long int.
As most of the users of this Forum are working with standard Arduinos (UNO, NANO, MEGA), the int type variable is known to them as a 16-bit memory space that can hold both positive and negative numbers of this range: 0x8000 to 0x7FFF (-32768 to 32767).
I found, somewhere in the Arduino Forum, the definition of "Standard Arduino" as having these three Learning Kits: Arduino UNO, Arduino NANO, and Arduino MEGA.
There have been many types of Arduino over the years, some officially branded Arduino and others, many of them from well known and respected names like Adafruit, Sparkfun etc. I don't think there is a "Standard" Arduino any more than there is a standard raspberry pi. There was, however a Standard car, and any other car is therefore non-standard
Actually, "Standard" was the make and they made quite a few models, so there isn't even a standard Standard!