Arduino和处理控制伺服

时间:2016-06-13 18:32:21

标签: arduino processing servo

我正在尝试使用处理控制伺服。

基本上,我想要的是按下处理按钮,使伺服从0到180度,每两秒后退一次,如果我按下另一个按钮,我应该可以用电位器移动伺服,但是我无法使其正常工作。

例如,当我按下POT按钮时,伺服转到所需的位置,但是如果我移动电位器,除非我再次点击锅按钮,否则伺服不会移动。

这是Arduino的相关代码:

   
    void loop ()
    {
      if(Serial.available()){
         val= Serial.read();
            while(val == 1){
              digitalWrite(ledCPM, HIGH);
              digitalWrite(ledPot, LOW);
              digitalWrite(ledFSR,LOW);
              cpmMovement();
              val = Serial.read();
              } 
     
            while(val == 2){
              digitalWrite(ledPot, HIGH);
              digitalWrite(ledCPM, LOW);
              digitalWrite(ledFSR,LOW);
              potMovement();
              val = Serial.read();
            }
            while(val == 3){
            digitalWrite(ledPot, LOW);
            digitalWrite(ledCPM, LOW);
            digitalWrite(ledFSR,HIGH);
            fsrMovement();
            val = Serial.read();
          }
      }
    }
      

      
    void cpmMovement(){unsigned long currentMillis = millis();

            if(currentMillis - previousMillis > interval){
              previousMillis = currentMillis;

              if(positionservo == 0){
                positionservo = 179;
                myservo1.write(positionservo);
              }
              else{
                positionservo = 0;
                myservo1.write(positionservo);
              }
            }
    }

void potMovement(){
  int analogValuePot = analogRead(Pot)/4;
        Serial.print("Potentiometer reading= ");
        Serial.println(analogValuePot); // This will print the raw force value
        int valuePot = map(analogValuePot, 0, 255, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
        myservo1.write(valuePot);  // sets the servo position according to the scaled value
        delay(10);                           // waits for the servo to get there
}
    void fsrMovement(){
            
            force = analogRead(FSR);  // Reads the FSR
          
            Serial.print("Force sensor reading = ");
            Serial.println(force); // This will print the raw force value
            int pos = map(force, 0, 1023, 0, 175); // Scales the force reading to degrees for servo control
            Serial.print("servomotor degrees = ");
            Serial.println(pos); // This will print the adjusted servo reading (an angle)
            myservo1.write(pos); // Write the new angle to the servo
            delay(150); // Delay 150 milliseconds before taking another reading
            
    }

在这里你可以看到相关的处理代码,因为其他一切正常

import processing.serial.*;


void mousePressed(){
  println("Coordinates: "  + mouseX + "," + mouseY);
  if(pressedCPMButton && currentColorCPM==butColorCPM){ //Changing color CPM to pressed button color
    currentColorCPM = butpreColorCPM;
    currentColorPOT = butColorPOT;
    currentColorFSR = butColorFSR;
    myPort.write(1);   //Send 1 to port
    
    }else if(pressedCPMButton && currentColorCPM==butpreColorCPM){
    currentColorCPM = butColorCPM;
    currentColorPOT = butColorPOT;
    currentColorFSR = butColorFSR;
  }
  if(pressedPOTButton && currentColorPOT==butColorPOT){
    currentColorCPM = butColorCPM;
    currentColorPOT = butpreColorPOT;
    currentColorFSR = butColorFSR;
    myPort.write(2);                                    //Send 2 to port
  }else if(pressedPOTButton && currentColorPOT==butpreColorPOT){
    currentColorCPM = butColorCPM;
    currentColorPOT = butColorPOT;
    currentColorFSR = butColorFSR;
  }
  if(pressedFSRButton && currentColorFSR==butColorFSR){
    currentColorCPM = butColorCPM;
    currentColorPOT = butColorPOT;
    currentColorFSR = butpreColorFSR;
    myPort.write(3);                                   //Send 3 to port
  }else if(pressedFSRButton && currentColorFSR==butpreColorFSR){
    currentColorCPM = butColorCPM;
    currentColorPOT = butColorPOT;
    currentColorFSR = butColorFSR;
  }
}

boolean pressedButtonCPM(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

boolean pressedButtonPOT(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
      return true;
  } else {
    return false;
  }
}

boolean pressedButtonFSR(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

0 个答案:

没有答案