Arduino reads serial port data and performs string segmentation

String comdata = "";
int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0;
void setup()
{
  for(int i = 0; i < 6; i++) pinMode(PWMPin[i], OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  int j = 0;
  while (Serial.available() > 0)
  {
    comdata += char(Serial.read());
    delay(2);
    mark = 1;
  }
  if(mark == 1)
  {
    Serial.println(comdata);
    Serial.println(comdata.length());
    for(int i = 0; i < comdata.length() ; i++)
    {
      if(comdata[i] == ',')
      {
        j++;
      }
      else
      {
        numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
      }
    }
    comdata = String("");
    for(int i = 0; i < 6; i++)
    {
      Serial.print("Pin ");
      Serial.print(PWMPin[i]);
      Serial.print(" = ");
      Serial.println(numdata[i]);
      analogWrite(PWMPin[i], numdata[i]);
      numdata[i] = 0;
    }
    mark = 0;
  }
}

Intelligent Recommendation

Arduino serial port accepts string operations

For the use of the Arduino serial port part, it is somewhat inconvenient. Although the function to be called is officially provided, it is not flexible enough. The following is a summary of the string...

Arduino serial port multi-byte reception, character segmentation

This blog takes the use of the 4G dtu network box in the human network as an example...

Arduino serial port accepts character data and transforms it

Arduino serial character conversion When doing Bluetooth acceptance experiments, the Arduino serial port receives all character data. Sometimes we want to perform operations on the data received by th...

Arduino and serial port interactively send data

Enter a, send humidity, enter b, send temperature...

More Recommendation

ARDUINO serial port output 16-enhanced data

Torting a morning, finally figured out the difference between serial.write and serial.print, the former is transferred between machines, the latter is to view, so transmitting characters....

Arduino data type serial port printout

Arduino data type serial port printout I chose the ESP8266 development board, NodeMCU 1.0, print test: Corresponding output: 19:45:42.135 -> 9999 19:45:43.615 -> 65535 19:45:45.136 -> 88888 1...

Use the serial port to receive data from Arduino

One example in "Love in Arduino" is to communicate with Arduino with the Processing program. The code is generally as follows, after modification: Still don't drive processing, try node I wa...

Arduino receives data sent by the serial port

1. We can send data to the computer through the Arduino for observation through the serial monitor. 2. You can call functionsSerial.available()To determine whether data is sent over, thereby receiving...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top