Hello everyone.
I’m trying hard to understand and adapt, several codes that, eventually, I find in the forums.
What I want to do:
1- Read and store, serial port data, in a variable, Example:
var1 = <000, 111, 222, 333, 444, 555, 666, 777, 888, 999, AAA…>
2- direct part of the variable to a new variable. Example:
var2 = 222, or var2 = var1 [7…9]
var5 = AAA, or var2 = var1 [31…33]
Searching, I saw that there is a user who published this code:
#include <Servo.h>
Servo myServo;
byte servoPin = 3;
byte servoMin = 10;
byte servoMax = 170;
byte servoPos = 0;
byte newServoPos = servoMin;
const byte numLEDs = 2;
byte ledPin[numLEDs] = {12, 13};
unsigned long LEDinterval[numLEDs] = {200, 400};
unsigned long prevLEDmillis[numLEDs] = {0, 0};
const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
char messageFromPC[buffSize] = {0};
int newFlashInterval = 0;
float servoFraction = 0.0; // fraction of servo range to move
unsigned long curMillis;
unsigned long prevReplyToPCmillis = 0;
unsigned long replyToPCinterval = 1000;
//=============
void setup() {
Serial.begin(9600);
// flash LEDs so we know we are alive
for (byte n = 0; n < numLEDs; n++) {
pinMode(ledPin[n], OUTPUT);
digitalWrite(ledPin[n], HIGH);
}
delay(500); // delay() is OK in setup as it only happens once
for (byte n = 0; n < numLEDs; n++) {
digitalWrite(ledPin[n], LOW);
}
// initialize the servo
myServo.attach(servoPin);
moveServo();
// tell the PC we are ready
Serial.println("<Arduino is ready>");
}
//=============
void loop() {
curMillis = millis();
getDataFromPC();
updateFlashInterval();
updateServoPos();
replyToPC();
flashLEDs();
moveServo();
}
//=============
void getDataFromPC() {
// receive data from PC and save it into inputBuffer
if(Serial.available() > 0) {
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker) {
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
}
}
//=============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
newFlashInterval = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
servoFraction = atof(strtokIndx); // convert this part to a float
}
//=============
void replyToPC() {
if (newDataFromPC) {
newDataFromPC = false;
Serial.print("<Msg ");
Serial.print(messageFromPC);
Serial.print(" NewFlash ");
Serial.print(newFlashInterval);
Serial.print(" SrvFrac ");
Serial.print(servoFraction);
Serial.print(" SrvPos ");
Serial.print(newServoPos);
Serial.print(" Time ");
Serial.print(curMillis >> 9); // divide by 512 is approx = half-seconds
Serial.println(">");
}
}
//============
void updateFlashInterval() {
// this illustrates using different inputs to call different functions
if (strcmp(messageFromPC, "LED1") == 0) {
updateLED1();
}
if (strcmp(messageFromPC, "LED2") == 0) {
updateLED2();
}
}
//=============
void updateLED1() {
if (newFlashInterval > 100) {
LEDinterval[0] = newFlashInterval;
}
}
//=============
void updateLED2() {
if (newFlashInterval > 100) {
LEDinterval[1] = newFlashInterval;
}
}
//=============
void flashLEDs() {
for (byte n = 0; n < numLEDs; n++) {
if (curMillis - prevLEDmillis[n] >= LEDinterval[n]) {
prevLEDmillis[n] += LEDinterval[n];
digitalWrite( ledPin[n], ! digitalRead( ledPin[n]) );
}
}
}
//=============
void updateServoPos() {
byte servoRange = servoMax - servoMin;
if (servoFraction >= 0 && servoFraction <= 1) {
newServoPos = servoMin + ((float) servoRange * servoFraction);
}
}
//=============
void moveServo() {
if (servoPos != newServoPos) {
servoPos = newServoPos;
myServo.write(servoPos);
}
}
APPARENTLY (I’m not sure), the author is the Robin2
I would be very grateful if someone could help me.