Go Down

Topic: MKRZero Serial communication (Read 2428 times) previous topic - next topic

StrikeforcePC

So  I am trying to send a serial command from my computer to my MKRZero.
I have C# code written which opens the COM port sends a ''1" then the MKRZero should move a servo and bring a pin high.
I think there must be something wrong with my Setup().
i have read something about Serial1 being used, but I'm not sure if that applies here or how to use it.
and it does execute in serial monitor when I send a 1.

This program ran great on an Arduino UNO


Code: [Select]
#include <Servo.h>
#define BIGSERVOSW 5
#define EXTRASERVOSW 4
#define ROCK1 2
#define ROCK2 3

#define ROCKETFIRE1 12
#define ROCKETFIRE2 11

#define LED 10

Servo DoorServo;
Servo servo2;
Servo servo3;

char c;
String cmd = "";
void setup() {
  // put your setup code here, to run once:
  pinMode(BIGSERVOSW, INPUT);
  pinMode(EXTRASERVOSW, INPUT);
  pinMode(ROCK1, INPUT);
  pinMode(ROCK2, INPUT);
  pinMode(ROCKETFIRE1, OUTPUT);
  pinMode(ROCKETFIRE2, OUTPUT);
  pinMode(LED, OUTPUT);
  DoorServo.attach(7);
  servo2.attach(6);
  servo3.attach(9);

  // LETS PULL UP THE INPUTS
  digitalWrite(BIGSERVOSW, HIGH);
  digitalWrite(EXTRASERVOSW, HIGH);
  digitalWrite(ROCK1, HIGH);
  digitalWrite(ROCK2, HIGH);
  DoorServo.write(0);
  servo2.write(0);
  servo3.write(0);

  Serial.begin(115200);
  while (!Serial);

  digitalWrite(ROCKETFIRE1, LOW);  //get back to default
  digitalWrite(ROCKETFIRE2, LOW);  //get back to default
  digitalWrite(LED, LOW);
}

void loop() {
  // put your main code here, to run repeatedly

  getSerial();
  // Check for bigservo switch
  if ( digitalRead(BIGSERVOSW) == LOW || cmd == "5")
  {
    /* according to "Then a Separate servo with its own button when pressed
      moves a Large servo 120 degrees then returns when you let of the button" */
    servo2.write(0);     //bring servo back to 0
    servo2.write(120);
    delay(500);
    Serial.println(" BIGSERVO 120deg arrived " );
    servo2.write(0);     //bring servo back to 0
    delay(500);     //for debounce
  }

  else if ( digitalRead(ROCK1) == LOW || cmd == "1")
  {
    Serial.println(" ROCK1 pressed" );
    DoorServo.write(95);  // open the door for 95 deg

    while ( DoorServo.read() != 95) //do nothing till servo come to 95
    {}
    delay(1000);         // dealy for 1 sec
    digitalWrite(ROCKETFIRE1, HIGH);  //fire the rocket 1
    delay(2000);          //delay for 2 sec
    digitalWrite(ROCKETFIRE1, LOW);  //get back to default
    delay(1000);
    DoorServo.write(0);  // close the door again 0deg
    while ( DoorServo.read() != 0) //do nothing till servo come to 0
    {}
    Serial.println(" Door closed" );
    delay(500);    //for debounce
    cmd = "";
  }

  else if ( digitalRead(ROCK2) == LOW || cmd == "2" )
  {
    Serial.println(" ROCK2 pressed" );
    DoorServo.write(95);  // open the door for 95 deg
    Serial.println(" Door opened" );
    while ( DoorServo.read() != 95) //do nothing till servo come to 95
    {}
    delay(1000);         // dealy for 1 sec
    digitalWrite(ROCKETFIRE2, HIGH);  //fire the rocket 1
    delay(2000);          //delay for 2 sec
    digitalWrite(ROCKETFIRE2, LOW);  //get back to default
    delay(1000);
    DoorServo.write(0);  // close the door again 0deg
    while ( DoorServo.read() != 0) //do nothing till servo come to 0
    {}
    Serial.println(" Door closed" );
    delay(500);    //for debounce
    cmd = "";
  }
  else if ( digitalRead(EXTRASERVOSW) == LOW || cmd == "3")
  {
    servo3.write(0);     //bring servo back to 0
    servo3.write(120);
    delay(500);
    Serial.println(" ExtraServo 120deg arrived " );
    servo3.write(0);     //bring servo back to 0
    delay(500);     //for debounce
  }

  if ( digitalRead(LED) == HIGH || cmd == "6" )
  {
    digitalWrite(LED, HIGH);
  }

  if ( digitalRead(LED) == LOW || cmd == "8" )
  {
    digitalWrite(LED, LOW);
  }

  if (digitalRead(ROCKETFIRE1) == HIGH || cmd == "7" ) //THIS IS THE FULL ASSAULT PROGRAM FIRING A LOT OF WEAPONS. VOICE ONLY.
  {
    DoorServo.write(95);  // open the door for 95 deg
    Serial.println(" Door opened" );
    while ( DoorServo.read() != 95) //do nothing till servo come to 95
    {}
    delay(1000);         // dealy for 1 sec
    digitalWrite(ROCKETFIRE1, HIGH);  //fire the rocket 1
    delay(1000);         // dealy for 1 sec
    digitalWrite(ROCKETFIRE2, HIGH);  //fire the rocket 2
    delay(1500);     //delay for 2 sec
    digitalWrite(ROCKETFIRE1, LOW);
    digitalWrite(ROCKETFIRE2, LOW);  // get back to default
    delay(2000);
    DoorServo.write(0);  // close the door again 0deg
    while ( DoorServo.read() != 0) //do nothing till servo come to 0
    {}
    Serial.println(" Door closed" );
    delay(500);    //for debounce
    cmd = "";
  }

}

void getSerial() {
  cmd = "";
  if (Serial.available() > 0) {
    cmd = "";
    c = Serial.read();
    cmd = c;
    while (Serial.available() > 0) {
      cmd.concat((char)Serial.read());
    }
  }
}

scamb09

Remove the line,


 while (!Serial);


I have found that the MKRZero controller hangs if the while loop is used and the application never runs.

This is strange however, as I have found the command documented in web examples, but it doesn't work.


With regards to 'Serial' & 'Serial1', the MKRZero 'Serial' command configures the virtual com port connection via the USB port.

'Serial1' configures a second serial port which can be accessed via pins 13 & 14 of the board.

Hope this helps.

Scamb09.


StrikeforcePC

The real solution to the issue was fixed.
Changing out the while (!Serial); helped.
but my C# was the problem.

To send a command from the PC to the aurduino using myport.WriteLine("6"); will only work with the UNO, Nano.

To send commands to the MKRZero one must use myport.Write("6"). 
Taking out "Line"

Thanks all for your help!

Go Up