Yep, you need to define your own tiny scripting language. Come up with the commands you want to recognize and start looking for the patterns once they arrive on the serial port. If the data received doesn't match any commands dispose data and start again.
You're familiar with the Arduino serial object?
Some pseudo-code...
char input[20];
byte i = 0;
loop() {
if serial.available() {
while (serial.available()) {
input = serial.read();
i++;*
if (i > 20) exit; // this isn't the right syntax but I can't remember what is right now*
I have figured out that I have to somehow use strtok to separate the serial data, and store it to individual variables. Being a n00b to scripting, I have no idea how to do this.
All I need, is it to store the character before the , to 1 variable, then store the value after the , to another variable.
If anyone can help me out with that, I would be grateful
I am controlling blower fans, relays etc, through some NPN transistors.
Say I have a fan on the left, and I wanted to turn it on at 500 (analogWrite value). I would type: l, 500
l would be defined as a certain pin in the script, and 500 is the analog value.
What I was thinking, is have a switch, which corresponded to the letter. Then once it had found which pin I wanted, it wrote the analog value using analogWrite, and loop that until I tell it differently.
Also, it would be good if I could have it maintain that value, even if I set other fans at different values.
So if I did: r, 500
the left fan would stay at the same value as I told it previously, and the r fan would also come on at 500 etc.
This is really very simple, if the commands are always in the form "c, nnn", where c is a lower case letter and n is a digit.
void loop()
{
char c, t;
int nnn;
if(Serial.available() > 0) // If there is data to read
{
c = Serial.read(); // Get a character
// Serial.print("c: ");
// Serial.println(c);
delay(50);
if(c >= 'a' && c <= 'z') // If it's a command
{
nnn = 0;
while(Serial.available() > 0) // While there is still data
{
t = Serial.read(); // Read next character
// Serial.print("t: ");
// Serial.println(t);
if(t == ',' || t == ' ')
continue; // Skip commas and spaces
if(t >= '0' && t <= '9') // If it's a number
{
nnn *= 10; // Multiply previous value by 10
nnn += t - '0'; // Add the new digit
}
else
break;
delay(50);
}
}
Serial.print("Cmd: ");
Serial.println(c);
Serial.print("Val: ");
Serial.println(nnn);
}
// Now, do something with c and nnn
switch(c)
{
case 'l':
// Turn left fan on (500 is not a valid value, by the way)
break;
case 'r':
// Turn right fan on, or off
break;
// Add other letters here, with break after each one
}
}
I tried this on my Arduino, with the serial monitor, and it works with simple commands like "r, 123" and "t, 1".
With the delay statements, data was grabbed by the Arduino faster than my computer could send it. I'm not sure that I understand why.
You could do nothing unless Serial.available() returned a value larger than 1, if you make sure that all values are 3 digit numbers, for instance.
for starters, what is the int 't' used for? to define the comma?
t = Serial.read(); // Read next character
t isn't an int. It is a char. It is the character read from the serial port, and, in this case, is supposed to contain the ASCII representation of a digit (0 through 9).
also, how does the arduino receive serial commands? one per loop? or can it only READ one command per loop?
Slowly. An interrupt fires every time there is serial data to be read. The appropriate interrupt handler is called, and collects a bunch of bits from the RX pin. The 8 bits are placed in a byte, and stored in the serial input buffer, where Serial.read() can read them. The Arduino can read any number of bytes from the serial buffer on any one pass through loop. On most passes, though, there is nothing to read.
another thing i didnt understand:
If the serial data coming in contains '1','3', '8', this code will set nnn to 1 ('1' - '0' = 1), then 10 + 3 ('3' - '0' = 3) which equals 13, then to 130 + 8 ('8' - '0' = 8) which equals 138.