Meshing things together (iOS - iPhone / Arduino Sketch)

Alright, I’ve reached another stopping point in my coding crusade sigh. First off want to give a big THANKS to the people who helped me in the prior thread I posted on here. That being said, I now have a blinking LED :smiley: Now what I want to do is make the LED blink on the kegboard-mini shield when I press the “Blink Flow_A” button in my iOS application. Now before you ask or begin to wonder, NO I am not using the redpark serial cable, YES my phone is jailbroken, YES I have serial communication working because I tested it with my RFID reader. I have successfully opened the serial port on my iPhone, and have read data being sent by a parallax RFID reader. …ohh and NO I don’t plan on releasing my app in the appstore …it’s free via github and a cydia repo.

Now that I have all that out of the way, basically I have a button setup in my iOS application that I would like to make an LED blink on a kegboard-mini shield, once the button is pressed.

The method in my iOS “app” looks like the following,

- (IBAction)blinkFlow_A_LED:(id)sender {
    
    // method to blink the Flow_A LED on the kegboard-mini Arduino sheild.
    
    NSLog(@"blink Flow_A btn pressed");
    
    // open serial port / interface
    
    [serial open:B2400];
    NSLog(@"%c", [serial isOpened]);
    
    // send serial data (tx)
    
    char buffer [7];
    
    buffer[0] = '{';
    buffer[1] = 'b';
    buffer[2] = 'l';
    buffer[3] = 'i';
    buffer[4] = 'n';
    buffer[5] = 'k';
    buffer[6] = '}';
    
    [serial write:buffer length:7];
}

The Arduino sketch I’m working with looks like the following,

// Simple sketch to make the Flow_A LED blink on a kegboard-mini Arduino shield

#define SOP '{'
#define EOP '}'


// serial stuff

bool started = false;
bool ended = false;

char inData[80];
byte index;

// LED stuff

int led = 4;

// the setup routine runs once when you press reset:
void setup()
{
  Serial.begin(2400);
  // Other stuff
  
  pinMode(led,OUTPUT);
}

// the loop routine run over and over again forever:
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?
  
  // turn on flow_A LED (hopefully)
  if(index == "{blink}")
  {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led LOW);
    delay(1000);
  }
  
  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';
  }
}

Now, I can’t upload the sketch because I am getting an error on the line that says,

if(index == '{blink}')

I guess my index is an integer, and I’m trying to compare it to a String which are two different data types …I know, but how could I run an if statement that does some sort comparison so it knows when it has read the buffer, with the following command to begin blinking the LED?

By looking at your code, I surmise that you are sending "{blink}" from the IPhone to the Arduino. The way to do this is to initialize a string variable, say inString, to "". Then each time a new alphabetic character arrives, say inChar, use

inString = inString + inChar.

Then replace your test condition "if (index == "{blink}", with a string comparison condition (read up on the string object in the reference) , and you ought to have this working.

Cheers,

John R. Doner

If you paid any attention to what the Arduino code is doing, you'd see that the characters are collected in inData, and that index is the number of characters in the array.

There is no way that index will equal '{blink}'.

If you had paid attention to the Arduino code, you'd also see that { and } are NOT stored in inData.

What you want is:

if(strcmp(inData, "blink") == 0)
{
   // It's blinky time
}