2019年7月2日 星期二

馬達轉速+button

byte motorPin=3;
int speed=0;
byte bnt1Pin = 4;
byte bnt2Pin = 5;
boolean state1;
boolean state2;
boolean buttonUp1;
boolean buttonUp2;
void setup()
{
  Serial.begin(9600);
  pinMode(bnt1Pin, INPUT);
  pinMode(bnt2Pin, INPUT);
  digitalWrite(bnt1Pin, HIGH);
  digitalWrite(bnt2Pin, HIGH);
}
void loop()
{
  if(digitalRead(bnt1Pin) == LOW && buttonUp1 == true)
  {
    speed=speed+10;
    Serial.println(speed);
    if(speed>250) speed=250;
    analogWrite(motorPin,speed);
    buttonUp1 = false;
  }
  else if(digitalRead(bnt1Pin) == HIGH && buttonUp1 == false)
  {
    buttonUp1 = true;
  }
  if(digitalRead(bnt2Pin) == LOW && buttonUp2 == true)
  {
    speed=speed-10;
    Serial.println(speed);
    if(speed<10) speed=10;       
    analogWrite(motorPin,speed);
    buttonUp2 = false;
  }
  else if(digitalRead(bnt2Pin) == HIGH && buttonUp2 == false)
  {
    buttonUp2 = true;
  }
  delay(10); // Delay 時間可視情況調整
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...