Serial interaction avoid led to blink

Hi.

In my code below, right after the loop block, there is a “digitalWrite(12, HIGH);” statement.

It simply turn on the led in the 12 pin.

Could someone explain why the led doesn’t turn on, unless I remove the ‘switch/case’ part that handle a servo motor?

It’s the ‘case 122:’ block.

If I remove, the led works fine.

As you can see the ‘digitalWrite(12,HIGH);’ is right at the beginning of the loop block…

The whole problem is a little bit worse, but at this time I’m only trying to understand this weird behavior… some problem among serial communication, servo and the variables it’s melting my brain… but at this point I would be glad if could understand why a so simple statement (digitalWrite(12,HIGH)) can be disturbed by any other code.

#include <Servo.h>

// 9 (nine) positions
int commands[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

// the servo motor
Servo myservo;

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

  // just for testing, the right sensor will be
  // chosen by software interaction
  pinMode(4,INPUT);
  
  // put some light in the serial interaction
  pinMode(12, OUTPUT);
}

void loop() {

  digitalWrite(12, HIGH);   
  
  // the value received by the serial interaction
  int character;
  
  // the value received by the serial interaction
  // that matters
  int value;

  value = 0;

  while (Serial.available()) {
    delay(5);

    character = Serial.read();

    // just for char/string treatment
    if (character >- '0' && character <= '9') {
      value = value * 10 + character - '0';
    }

    // now what matters
    switch ( character ) {
      // array start. If defined, reset all values
      // 97 means the 'a' char in the ascii table
    case 97:
      //reset the whole array commands[]";
      commands[0] = 97;
      // what motor
      commands[1] = 0;
      // which direction (clockwise/counter)
      commands[2] = 0;
      // motor destiny/angle
      commands[3] = 0;
      // what sensor 1
      commands[4] = 0;
      // sensor 1 must be on or off?
      commands[5] = 0;      
      // what sensor 2
      commands[6] = 0;
      // sensor 2 must be on of off?
      commands[7] = 0;      
      // end of the array
      commands[8] = 0;      
      break;

      // '122' is the 'z' char in the ascii table
      // 'z' is the o end-of-line of the command to fill the array.
      // '35z' for example means use the value 35
      // if the array is full (all positions has values), it's time to execute what matters
      // if the array has any zero value, keep filling the array
    case 122:
      if ( commands[1] == 0 ) {
        // Again, 'value' is the value sent right before 'z'. Example '9z'.
        // So, if commands[0] is empty, we have to fill the position with the value.
        // If commands[0] isnt empty, it has already filled in a prior serial interaction
        commands[1] = value;
        value = 0;
        
        // keep in the while (Serial.available()) block
        continue;
      }

      if ( commands[2] == 0 ) {
        commands[2] = value;
        value = 0;
        continue;
      }

      if ( commands[3] == 0 ) {
         commands[3] = value;
        value = 0;
        continue;
      }


      if ( commands[4] == 0 ) {
        commands[4] = value;
        value = 0;
        continue;
      }     

      if ( commands[5] == 0 ) {
        commands[5] = value;
        value = 0;
        continue;
      }

      if ( commands[6] == 0 ) {
        commands[6] = value;
        value = 0;
        continue;
      }

      if ( commands[7] == 0 ) {
        commands[7] = value;
        value = 0;
        continue;
      }


      
      // NOW we have filled all the array positions.
      // So we take the values and do what we have to do.
      if ( commands[8] == 0 ) {           
        commands[8] = 122;

        value = 0;
        
        // turn the lights on to show we reach the end of the interaction
        digitalWrite(12, HIGH);        

        // only do stuff if the motor is already attached.
        if ( ! myservo.attached() ) {
          Serial.println("------- NOT ATTACHED ----");
          myservo.attach(9);
        }
        else {
          Serial.println("------- YES ATTACHED ----");
          Serial.println("turn... turn... turn...");
          
          // look at the switch situation
          int sens0 = 0;
            if ( digitalRead(4) == HIGH ) {
              sens0 = 1;
            }
            else {
              sens0 = 0;
            }          

          // just blah blah blah.
          // It will be defined better soon
          Serial.print("a,");
          Serial.print(sens0);
          Serial.print(",");    
          Serial.println("z");  

          // just turn, please, turn...
          myservo.write(50);
          delay(5);
          myservo.write(0);  

          Serial.println("TURNING FINISHED!!!");      
          Serial.println("OK");

        }
      }
      break;
    }
  }

}
    if (character >- '0' && character <= '9') {

Greater than -‘0’?

      // 97 means the 'a' char in the ascii table
    case 97:

OK. On the other hand,

case 'a':

doesn’t need a comment to describe any relationship.

      commands[0] = 97;

No.

      commands[0] = 'a';

Why are you not just attaching the servo in setup()?

Why are you not just attaching the servo in setup()?

Because it's about five motors. I will attach the motor dinamically, as specified in one of the array values.

In the code I've put here I'm using a literal value (9) to attach just to make easier to read.

Your modifications didn't make any difference in the result... the led still off.

But thank you for your attention.

[]s Alexander Brazil - Rio de Janeiro

Because it's about five motors. I will attach the motor dinamically, as specified in one of the array values.

If the servos are not attached, they can drift. Is this OK in your project?

Add a Serial.print() statement to setup(). It is possible that your strange serial data collection is causing a reset.

If the servos are not attached, they can drift. Is this OK in your project?

I didn't think about that... well... I'll check again the whole idea.

Add a Serial.print() statement to setup().

I'll do that and check the result.

It is possible that your strange serial data collection is causing a reset.

"strange serial data collection"?

Ok, I'll check this too.

In other words, do all the code again. No problem.

Thank you again.

[]s Alexander Brazil - Rio de Janeiro

“strange serial data collection”?

Yes. You should collect all the serial data that defines a packet, then parse that data to populate the array.

#define SOP <
#define EOP >

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

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

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

Add your code to parse and use the serial data after the comment // The end of packet…

THANK YOU for the code!

I'm using it!

[]s Alexander Brazil - Rio de Janeiro