My main aim is to send a list from python to the arduino and store it in an arduino array of numbers.So that I can move actuators according to the received data.
I am working on example 3 and my aim is to send numbers from python( as strings) and receive them from arduino as integers. The code was altered like down below so I can do what is asked of. The problem here is that I can print them on the serial monitor. However, when I try to store them I get the numbers 0 printed out. ( I do the printing on python.)
The code down below allows the arduino to print the received data from python.
// Example 3 - Receive with start- and end-markers
const byte numChars = 1000;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
int i;
long int sayi=0;
long int sayilar[100];
if (newData == true) {
for(i=0;i<sizeof(receivedChars)-1;i++){
Serial.flush();
if (receivedChars[i]!=','){
sayi=sayi*10+receivedChars[i]-48;
}
else{
Serial.println(sayi);
sayi=0;
}
}
}
newData = false;
}
This works perfectly. However, when I try to store the data in an array and serial print the array I only get the first data or I just get a whole set of 0s.
This is how I was trying to store the array and print it:
// Example 3 - Receive with start- and end-markers
// I have only changed this function so I am putting it here instead of the whole thing.
void showNewData() {
int i;
long int sayi=0;
long int sayilar[100];
if (newData == true) {
for(i=0;i<sizeof(receivedChars)-1;i++){
Serial.flush();
if (receivedChars[i]!=','){
sayi=sayi*10+receivedChars[i]-48;
for (i=0;i<sizeof(receivedChars)-1;i++){
sayilar[i]=sayi; //storing the variable sayi into an array
}
}
else{
for (i=0;i<sizeof(sayilar)-1;i++){
Serial.println(sayilar[i]);
}
sayi=0;
}
}
newData = false;
}
}
These codes are very identical and it can be seen that the only part that was changed from the first code was that I have tried was to store the numbers in an array.
I am looking for advice here. Maybe some other guides on how to approach the problem would be better.
Anyway looking forward to your feedback on this topic.
The SafeString library has multiple examples of reading the Serial input and tokenizing and converting to numbers.
Checkout the examples in the library and the detailed tutorial
pcRead () demonstrates how to read and process both numeric and single character commands from the serial interface. digits are accumulated to create multidigit values and alpha characters terminate and determine how a numeric value is handled.
you could send a series of numbers followed by an alpha terminator that stores the number. a separate character can invoke a function to dump the values
// pcRead - debugging using serial monitor
const char version [] = "PcRead 201114a";
int debug = 0;
// ---------------------------------------------------------
// toggle output bit
void
pinToggle (
int pin)
{
static int bits = 0;
int bit = 1 << pin;
if (debug) {
Serial.print ("pinToggle: ");
Serial.println (pin);
}
if (bits & bit) {
digitalWrite (pin, LOW);
bits &= ~bit;
}
else {
digitalWrite (pin, HIGH);
bits |= bit;
}
}
// ---------------------------------------------------------
// toggle output bit
int
readString (
char *s,
int maxChar )
{
int n = 0;
Serial.print ("> ");
do {
if (Serial.available()) {
int c = Serial.read ();
if ('\n' == c)
break;
s [n++] = c;
if (maxChar == n)
break;
}
} while (true);
return n;
}
// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR 10
char s [MAX_CHAR] = {};
int analogPin = 0;
void
pcRead (void)
{
static int val = 0;
if (Serial.available()) {
int c = Serial.read ();
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
val = c - '0' + (10 * val);
break;
case 'A':
analogPin = val;
Serial.print ("analogPin = ");
Serial.println (val);
val = 0;
break;
case 'D':
debug ^= 1;
break;
case 'I':
pinMode (val, INPUT);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" INPUT");
val = 0;
break;
case 'O':
pinMode (val, OUTPUT);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" OUTPUT");
val = 0;
break;
case 'P':
pinMode (val, INPUT_PULLUP);
Serial.print ("pinMode ");
Serial.print (val);
Serial.println (" INPUT_PULLUP");
val = 0;
break;
case 'a':
Serial.print ("analogRead: ");
Serial.println (analogRead (val));
val = 0;
break;
case 'c':
digitalWrite (val, LOW);
Serial.print ("digitalWrite: LOW ");
Serial.println (val);
val = 0;
break;
case 'p':
#if !defined(ARDUINO_ARCH_ESP32)
analogWrite (analogPin, val);
Serial.print ("analogWrite: pin ");
Serial.print (analogPin);
Serial.print (", ");
Serial.println (val);
val = 0;
#endif
break;
case 'r':
Serial.print ("digitalRead: pin ");
Serial.print (val);
Serial.print (", ");
Serial.println (digitalRead (val));
val = 0;
break;
case 's':
digitalWrite (val, HIGH);
Serial.print ("digitalWrite: HIGH ");
Serial.println (val);
val = 0;
break;
case 't':
Serial.print ("pinToggle ");
Serial.println (val);
digitalWrite (val, ! digitalRead (val));
val = 0;
break;
case 'v':
Serial.print ("\nversion: ");
Serial.println (version);
break;
case '\n': // ignore
break;
case '"':
while ('\n' != Serial.read ()) // discard linefeed
;
readString (s, MAX_CHAR-1);
Serial.println (s);
break;
case '?':
Serial.println ("\npcRead:\n");
Serial.println (" [0-9] append to #");
Serial.println (" A # - set analog pin #");
Serial.println (" D # - set debug to #");
Serial.println (" I # - set pin # to INPUT");
Serial.println (" O # - set pin # to OUTPUT");
Serial.println (" P # - set pin # to INPUT_PULLUP");
Serial.println (" a # - analogRead (pin #)");
Serial.println (" c # - digitalWrite (pin #, LOW)");
Serial.println (" p # -- analogWrite (analogPin, #)");
Serial.println (" r # - digitalRead (pin #)");
Serial.println (" s - digitalWrite (pin #, HIGH)");
Serial.println (" t -- toggle pin # output");
Serial.println (" v - print version");
Serial.println (" \" - read string");
Serial.println (" ? - list of commands");
break;
default:
Serial.print ("unknown char ");
Serial.println (c,HEX);
break;
}
}
}
// -----------------------------------------------------------------------------
void
loop (void)
{
pcRead ();
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin(115200);
Serial.println (version);
#if defined(ARDUINO_ARCH_ESP32)
Serial.println ("esp32");
#endif
}