Time based servo Movement using Arduino Yún

I'm using an arduino Yún to get the time from a network.

I've created functions that should select a servo angle from a global array of angle values based on the time.

The functions work when I use LEDs instead of servos.

What seems to happen here is that an initial time in the serial monitor is given and then the arduino freezes.

I've attached a code snippet of a condensed version of what I'm trying to do.

Any help would be appreciated.

Josh

#include <Process.h>
#include <Servo.h>

Servo myServo1;

Process date;                 // process used to get the date
int hours, minutes, seconds;  // for the results
int lastSecond = -1;          // need an impossible value for comparison

int originAngle = 0;

int angles [9] ={25,40,55,70,75,90,105,120,135};

int switchState = 0;
void setup() {
  Bridge.begin();        // initialize Bridge
  // run an initial date process. Should return:
  // hh:mm:ss :
  if (!date.running()) {
    date.begin("date");
    date.addParameter("+%T");
    date.run();
  }
  pinMode(2,INPUT);

  myServo1.attach(3);
}


uint8_t  servo1(int hours){
  int h1;
//SERVO 1 ------------
 if(hours >=0 && hours <=9){
  h1 = 0; 
 return h1;
 }else if (hours >= 10 && hours <=19){
   h1 = 1; 
 return h1;
 }else if (hours >= 20 && hours<=24 ){
   h1 = 2; 
 return h1;
 }
}

void servoMove1(int hours){
  if(servo1(hours) == 0){
    myServo1.write(originAngle);
  }else if(servo1(hours) == 1){
    myServo1.write(angles[0]);
  }else if(servo1(hours) == 2){
    myServo1.write(angles[1]);
  }
}



void noServo(){
myServo1.write(originAngle);
}

void loop() {

switchState = digitalRead(2);

if(switchState == HIGH){
 servoMove1(hours);
}else{
  noServo();
}

  
  if (lastSecond != seconds) { // if a second has passed
    // print the time:
    if (hours <= 9) {
      SerialUSB.print("0");  // adjust for 0-9
    }
    SerialUSB.print(hours);
    if (minutes <= 9) {
      SerialUSB.print("0");  // adjust for 0-9
    }
    SerialUSB.print(minutes);
  
    // restart the date process:
    if (!date.running()) {
      date.begin("date");
      date.addParameter("+%T");
      date.run();
    }
  }

  //if there's a result from the date process, parse it:
  while (date.available() > 0) {
    // get the result of the date process (should be hh:mm:ss):
    String timeString = date.readString();

    // get the substrings for hour, minute second:
    String hourString = timeString.substring(0);
    String minString = timeString.substring(3);

    // convert to ints,saving the previous second:
    hours = hourString.toInt();
    minutes = minString.toInt();
    return hours;
    return minutes;
  }

}

Problem solved.

Servo wasn't working connected to 5v for some reason and I removed the delays.

Here is the code:

If anybody thinks of a better way to do this please let me know

#include <Process.h>
#include <Servo.h>

Servo myServo1;

Process date;                 // process used to get the date
int hours, minutes, seconds;  // for the results
int lastSecond = -1;          // need an impossible value for comparison

int originAngle = 0;

int angles [9] ={25,40,55,70,75,90,105,120,135};

int switchState = 0;
void setup() {
  Bridge.begin();        // initialize Bridge
  // run an initial date process. Should return:
  // hh:mm:ss :
  if (!date.running()) {
    date.begin("date");
    date.addParameter("+%T");
    date.run();
  }
  pinMode(2,INPUT);

  myServo1.attach(4);
}


uint8_t  servo1(int hours){
  int h1;
//SERVO 1 ------------
 if(hours >=0 && hours <=9){
  h1 = 0; 
 return h1;
 }else if (hours >= 10 && hours <=19){
   h1 = 1; 
 return h1;
 }else if (hours >= 20 && hours<=24 ){
   h1 = 2; 
 return h1;
 }
}

void servoMove1(int hours){
  if(servo1(hours) == 0){
    myServo1.write(angles[8]);
  
  }else if(servo1(hours) == 1){
    myServo1.write(angles[7]);

  }else if(servo1(hours) == 2){
    myServo1.write(angles[6]);

  }
}



void noServo(){
myServo1.write(originAngle);

}

void loop() {


switchState = digitalRead(2);
if(switchState == HIGH){
 servoMove1(hours);
}else if (switchState == LOW){
  noServo();
}

  
  if (lastSecond != seconds) { // if a second has passed
    // print the time:
    if (hours <= 9) {
      SerialUSB.print("0");  // adjust for 0-9
    }
    SerialUSB.print(hours);
    if (minutes <= 9) {
      SerialUSB.print("0");  // adjust for 0-9
    }
    SerialUSB.print(minutes);
  
    // restart the date process:
    if (!date.running()) {
      date.begin("date");
      date.addParameter("+%T");
      date.run();
    }
  }

  //if there's a result from the date process, parse it:
  while (date.available() > 0) {
    // get the result of the date process (should be hh:mm:ss):
    String timeString = date.readString();

    // get the substrings for hour, minute second:
    String hourString = timeString.substring(0);
    String minString = timeString.substring(3);

    // convert to ints,saving the previous second:
    hours = hourString.toInt();
    minutes = minString.toInt();
    return hours;
    return minutes;
  }

}