Back to Parent

int flowR1 = 5000;
int flowR2 = 5000;
int min = 1000;
int max = 8000;
int thresh = 500;
bool num = true;
int comp;
int negthresh;
int status;
int flowSpeed;
int motorPin = A0;
int motorSpeed;
int buttonPin = D1;

int servoPin = A7;
Servo myServo;
int servoPos = 0;

int neut = 90;
int rise = 130;
int fall = 40;

void setup()
{
   Spark.function("flow", flowF);
   //Spark.variable("flow1", &flowR1, INT);
   //Spark.variable("flow2", &flowR2, INT);
   Spark.variable("status", &status, INT);
   //constrain(flowR1, min, max);
   //constrain(flowR2, min, max);
   myServo.attach( A7 );
   pinMode(buttonPin, INPUT);
}

void loop()
{
  if (num == true) {
    comp = flowR1-flowR2;
    negthresh = -thresh;
    if (abs(comp) <= thresh) {
      //neutral servo
      myServo.write(neut);
      status = 0;
    }
    else if (comp < negthresh) {
      //rising servo
      myServo.write(rise);
      status = 1;
    }
    else if (comp > thresh) {
      //falling servo
      myServo.write(fall);
      status = -1;
    }
  }

  else if (num == false) {
    comp = flowR2-flowR1;
    negthresh = -thresh;
    if (abs(comp) <= thresh) {
      //neutral servo
      myServo.write(neut);
      status = 0;
    }
    else if (comp < negthresh) {
      //rising servo
      myServo.write(rise);
      status = 1;
    }
    else if (comp > thresh) {
      //falling servo
      myServo.write(fall);
      status = -1;
    }
  }
  int button = digitalRead(buttonPin);
  if (button == HIGH) {
    digitalWrite(motorPin, HIGH);
  }
  else{
    digitalWrite(motorPin, LOW);
  }
}


int flowF(String command)
{
  if (num == true) {
   flowR1 = command.toInt();
 }
 else if (num == false) {
   flowR2 = command.toInt();
 }
 num = !num;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0