2

The program listen to messages from serial port in the form or where first character (A or D) means analog or digital, the 2nd character - pin, the 3rd character - 1/0 or from 0 to 255. The markers < and > show the beginning and the end of packet.

For example, if packet is received, the light is turned on by digitalWrite(13,1) But nothing happens. When I send via serial monitor, for instance: light is supposed to blink but it does not. The same with analogue outputs.

bool started = false;
bool ended = false;
char inData[5];
byte index;


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
      break;
    }

    else
    {
      if (index <= 4)
      {
        inData[index] = inChar;
        index++;
      }
    }



     if (started && ended)
    {
      if (inData[0] == 'A')
      {
        pinMode(inData[2],OUTPUT);
        analogWrite(inData[2],inData[4]);
      }


      else if (inData[0] == 'D')
      {
        if (inData[4] == 1)
        {
          pinMode(inData[2],OUTPUT);
          digitalWrite(inData[2],HIGH);
        }

        else if (inData[4] == 0)
        {
           pinMode(inData[2],OUTPUT);
           digitalWrite(inData[2],LOW);
        }
      }
      started = false;
      ended = false;
      index = 0; 
    }

  }


Serial.println("Sending");  



}
user1165574
  • 167
  • 5
  • 13

4 Answers4

4

The following code will allow you to execute a method with an example serial string:

<power,led>

Once it processes this string, it'll execute the following method:

sendCommand(cmd, val);

See below for an example of how to turn on an LED on PIN 11.

#include <avr/pgmspace.h>

int IRledPin = 11;

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup() {
  pinMode(IRledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Read all serial data available, as fast as possible
  while (Serial.available() > 0) {
    char inChar = Serial.read();

    if (inChar == SOP) {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    } else if (inChar == EOP) {
       ended = true;
       break;
    } else {
      if (index < 79) {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if (started && ended) {
    // The end of packet marker arrived. Process the packet
    char *cmd = strtok(inData, ",");

    if (cmd) {
       char *val = strtok(NULL, ",");
       if (val) {
          sendCommand(cmd, val);
       }
    }

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

void sendCommand(char *command, char *value) {
  if (strcmp(command,"power") == 0) {
    power(value);
  }
}

void power(char* value) {
  if (strcmp(value, "led") == 0) {
    digitalWrite(IRledPin, HIGH);
  }
}
gotnull
  • 26,454
  • 22
  • 137
  • 203
1

If the 2nd character is the pin, then you want inData[1] for your pin numbers instead of inData[2].

Tim
  • 14,447
  • 6
  • 40
  • 63
  • [0] is D, [1] is comma, [2] is 0. – user1165574 Jan 26 '12 at 01:26
  • You didn't specify that you're using commas. – Tim Jan 26 '12 at 01:27
  • Sorry. The message looks like this or – user1165574 Jan 26 '12 at 01:28
  • The problem then is that `inData[2]` for `` is going to be the char representation of the char '1', which is actually 49. – Tim Jan 26 '12 at 01:33
  • Hm...How can I fix it? And why represenation of char '1"? – user1165574 Jan 26 '12 at 01:36
  • 1
    Because the number `1` is typed as a character, the same way you'd type `!`, `Q`, or `,`. You'll need to input your entire array of chars, then convert it to a string, then use a string splitting method to get the individual items, then use an int parser to get the integers out of the substrings. – Tim Jan 26 '12 at 01:38
  • The data your reading in from the serial is in ASCII and comma separated too. The index isn't counting the comma's. – Hellonearthis Jan 26 '12 at 05:19
1
  • Why do you go from inData[0] to inData[2]? Wouldn't the second character be in inData[1]?

  • You're setting the pinMode to the actual value of inData[2]. That means to turn on pin 13, you need to send a carriage return character ('\r').

Borealid
  • 95,191
  • 9
  • 106
  • 122
-1

The code doesn't run below but it should help you to sort out your problem.

What it tries to do is split the inData into the Tokens[] array.

It then turns the ASCII data into integers with the atoi() statement.

Hope it helps.

Got the splitter from Segmentation Fault when using strtok_r

bool started = false;
bool ended = false;
char inData[5];
byte index;


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
      break;
    }

    else
    {
      inData[index] = inChar;
      index++;
    }


     if (started && ended)
    {

// https://stackoverflow.com/questions/2227198/segmentation-fault-when-using-strtok-r
// Splint up input data
    char *p = inData;
    char *tokens[50];
    int i = 0;

    while (i < 50) {
      tokens[i] = strtok_r(p, ",", &p);
      if (tokens[i] == NULL) {
         break;
         }
      i++;
    }

    if (tokens[0] == '<A')
      {
        pinMode(tokens[1],OUTPUT);
        analogWrite(tokens[2],tokens[3]);
      }


      else if (token[0] == '<D')
      {
        if (atoi(token[3]) == 1)
        {
          pinMode(atoi(token[1]),OUTPUT);
          digitalWrite(atoi(token[1]),HIGH);
        }

        else if (atoi(tokens[3]) == 0)
        {
           pinMode(atoi(tokens[1]),OUTPUT);
           digitalWrite(atoi(tokens[1]),LOW);
        }
      }
      started = false;
      ended = false;
      index = 0; 
    }

  }
Community
  • 1
  • 1
Hellonearthis
  • 1,664
  • 1
  • 18
  • 26