Hi guys,
I have to take in input from a serial connection an array of 200 int, how can I do?
The command Serial.read() does not work because it seems that it can transmit only a single value and not an array.
Thank you
Welcome to the forum
What range of values do the values being read have ?
Are you receiving binary values or ASCII representations of the values ?
think we require more details of the project
is the data in text form or binary? give us an example?
what is the source of the data? host PC, attached serial device, etc?
what Arduino are you planning to use?
have a read of arduino serial-input-basics-updated
I have to power addressable leds based on the relative orientation between the Sun and the considered vehicle.
I have a Simulink model in which I solve the vehicle dynamic and find the relative angle, than I generate an array of 100 elements (because I have a 100 led string) in which each element represents the intensity of each led (between 0 and 100).
I’d like to send for each time instant a 1x100 array by the Simulink block Serial transmit (in the Simulink support package for Arduino hardware).
I wrote the following code but I got error during the compilation step:
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 100
#define PINK CRGB(40, 220, 170)
int vettore[NUM_LEDS];
int MaxBright;
CRGB leds[NUM_LEDS];
void setup(){
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds,NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 250);
FastLED.clear();
FastLED.show();
}
void loop() {
vettore = Serial.read();
}
I get the following error:
C:\Users\HP\AppData\Local\Temp.arduinoIDE-unsaved20221030-33068-3d88w1.c7mc7\sketch_nov30f\sketch_nov30f.ino: In function 'void loop()':
C:\Users\HP\AppData\Local\Temp.arduinoIDE-unsaved20221030-33068-3d88w1.c7mc7\sketch_nov30f\sketch_nov30f.ino:20:23: error: incompatible types in assignment of 'int' to 'int [100]'
vettore = Serial.read();
^
exit status 1
Compilation error: incompatible types in assignment of 'int' to 'int [100]'
First, if all values are in the range 0-100, then the byte type is enough to store them, and you need to describe your array accordingly. You have it described as int - but int is two bytes, so you need to read not 100 bytes for the array, but all 200.
Next - if the read command reads one byte, and you need to read 100 - so you need to write a loop and read 100 times by one byte in a loop, and that's all
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
vettore = Serial.read();
vettore is declared as an array. You cannot assign a value to it like that, hence the error
We need more details of exactly what you are receiving and its format in order to provide more help
are the integers generated by Simulink and transmitted over serial terminated by newline, seperated by a space, seperated by a comma, ????
assuming terminated by newline the following may be a start
void loop() {
static int i=0;
if(Serial.available()) {
vettore[i++] = Serial.parseInt();
}
}
you need an index to access the array elements (i in the above) - as each new element arrives it is stored and the index incremented
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.