Berrak Şişman & İlker Çam
Arduino code:
const int btn1Pin = 2; const int btn2Pin = 3; const int motorPin = 9; int theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 1; int motorSpeed = 0; void setup() { pinMode(motorPin, OUTPUT); pinMode(btn1Pin, INPUT); pinMode(btn2Pin, INPUT); Serial.begin(9600); } void loop() { //serialSpeed(); motor(); } void motor(){ int btnState, btn2State; btnState = digitalRead(btn1Pin); btn2State = digitalRead(btn2Pin); if(btnState == LOW && btn2State == HIGH && theBitThatIReadFromSerialIfTheMotorIsRunningOrNot == 1){ motorSpeed = (motorSpeed+40); motorSpeed = constrain(motorSpeed,0,255); analogWrite(motorPin, motorSpeed); }else if(btnState == HIGH && btn2State == LOW && theBitThatIReadFromSerialIfTheMotorIsRunningOrNot == 1){ motorSpeed = (motorSpeed-40); motorSpeed = constrain(motorSpeed,0,255); analogWrite(motorPin, motorSpeed); } if(Serial.available() > 0){ int readed = Serial.parseInt(); if(readed == 1){ theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 1; if(motorSpeed<=0){ motorSpeed = 200; analogWrite(motorPin, motorSpeed); } }else if(readed == 0){ theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 0; if(motorSpeed>0){ motorSpeed = 0; analogWrite(motorPin, motorSpeed); } } } delay(100); }
No comments:
Post a Comment