HND: Live GPS and Vibe

Slightly updated code for incorporating vibe control via serial.


[Processing: HND LiveGPS, Weight and Vibe]
Arduino Code:


int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse,pulseR;

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int distance=0;
int angle=0;

//vibe
int vibeMotor0 = 0;
int vibeMotor1 = 0;
int vibeMotor2 = 0;
int vibeMotor3 = 0;
int vibe;

void setup() {
  pinMode(11, OUTPUT);  // Set servo pin as an output pin
  pinMode(10, OUTPUT); 

  //vibe
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);

  pulse = minPulse;           // Set the motor position value to the minimum
  pulseR = minPulse;
  Serial.begin(9600);
  Serial.print(65);
}

void loop() {

  if (Serial.available()>=4) {
    int serialIn=Serial.read();
    if(serialIn==255){
      distance=Serial.read();
      angle=Serial.read();
      vibe=Serial.read();
    }

    //if(distance<20){
    if (vibe == 0)  {
      digitalWrite(3, HIGH);
      //delay(100);
      //digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);

    }

    if (vibe == 1)  {
      digitalWrite (3, LOW);
      digitalWrite(5, HIGH);
      //delay(100);
      //digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);

    }

    if (vibe == 2)  {
      digitalWrite (3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      //delay(100);
     // digitalWrite(6, LOW);
      digitalWrite(9, LOW);

    }

    if (vibe == 3)  {
      digitalWrite (3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(9, HIGH);
      //delay(100);
      //digitalWrite(9, LOW);

    }
        if (vibe == 4)  {
      digitalWrite (3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);
      //delay(100);
      //digitalWrite(9, LOW);

    }
    //}
  }

  pulse = distance*4*19/10+minPulse;    // convert the analog value
  pulseR = angle*4*19/10+minPulse;    // convert the analog value

    // pulse the servo again if rhe refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(11, HIGH);   // Turn the motor on
    delayMicroseconds(pulseR);       // Length of the pulse sets the motor position
    digitalWrite(11, LOW);    // Turn the motor off
    digitalWrite(10, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(10, LOW);    // Turn the motor off 

    lastPulse = millis();           // save the time of the last pulse
  }

  Serial.print(255,BYTE); //send something
}