Hi, I am trying to generate an array of 1000 values with Random() function. I want to save all the 1000 values into the array first and then print it once full. I am getting a result with a Serial.println in the for loop, but i want the result to come in one go as i want the sampling time to be as small as possible. Any possible help would be appreciated.
float a[1000] ;
int i;
void setup()
{
Serial.begin(9600);
}
void loop() {
for ( i = 0; i < 1000 ; i++) {
a[i] = random(8,15);
}
for (i = 0; i < 1000; i++); {
Serial.print (a[i]);
}
Serial.println(" End");
}
What do you mean by "sampling time'? What is being sampled here?
The speed of your code here is limited by the baud rate, 9600. The time taken to generate the random numbers is tiny by comparison, so saving them to an array won't make any noticeable difference.
Hi Paul, Thanks for the reply. I dont really need the input with decimals. I used float just so that i have more memory than with int (Guess that was a terrible move). My ultimate aim is to input the values into the arduino through a port and generate a graph with the values recieved through it. What i am currently doing with the program is a sample, simulating the values recieved through the port using a random() function.
By sampling time i mean the graph which i need to generate has a time period of 1.5ms. So i would need the sampling of the board atleast half of that (which i am no way near)
Note: i use an Expressif ESP32 board.
Thanks Idahowalker. When i use your code, i am getting a lot of 1s and 0s. I will attach the code below with my edits.
int count = 0;
int maxCount = 100;
int i;
int a[100];
void setup() {
Serial.begin(9600);
}
void loop() {
a[count] = random(8, 15);
count++;
if ( count >= maxCount )
{
//do the print thingy
for (i = 0; i < maxCount; i++); {
Serial.print (a[i]);
}
count = 0; // put count back to 0
}
}
By declaring int i as a global if, after the for loop has ran once and max count is equal to 100 and i is not set back to 0 before the next for loop is ran, the for loop thingy will not work as expected because i still has a 100 in it.
what do you mean you will HAVE more memory?? The memory is fixed, float takes 4 bytes, int takes 2 bytes, so using float instead of int when you don’t really need it you DOUBLE your memory usage for no reason
@andsur is using esp32, so int will be 4 bytes. Not sure how many bytes float will be on esp32. But whatever, esp has plenty of ram for 1000 array indexes.