Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Servo.h>
- Servo servo;
- int buttonLeft = 2;
- int buttonRight = 3;
- const int ledGreen = 7;
- const int ledRed = 8;
- int timesPressed = 0;
- int angleDirection = 0;
- float desiredAngle = 90;
- float currentAngle = 80;
- long randomNumber;
- const int potentioPin = A0;
- int potentioVal = 0;
- void setup() {
- Serial.begin(9600);
- servo.attach(11);
- pinMode(buttonLeft, INPUT);
- pinMode(buttonRight, INPUT);
- pinMode(ledGreen, OUTPUT);
- pinMode(ledRed, OUTPUT);
- attachInterrupt(digitalPinToInterrupt(buttonLeft), rotateServo, FALLING);
- }
- void loop() {
- if(digitalRead(buttonLeft) == 0 && digitalRead(buttonRight) == 0){
- digitalWrite(ledGreen, HIGH);
- } else {
- digitalWrite(ledGreen, LOW);
- }
- if(angleDirection == -1){
- if(currentAngle > desiredAngle){
- currentAngle += angleDirection * round(potentioVal / 30);
- servo.write(round(currentAngle));
- }
- } else {
- if(currentAngle < desiredAngle){
- currentAngle += angleDirection * round(potentioVal / 30);
- servo.write(round(currentAngle));
- }
- }
- delay(100);
- if(currentAngle == desiredAngle){
- Serial.println("Reached position of motor");
- digitalWrite(ledRed, HIGH);
- delay(2000);
- digitalWrite(ledRed, LOW);
- }
- }
- void rotateServo() {
- Serial.println("Button was pushed");
- timesPressed += 1;
- potentioVal = analogRead(potentioPin);
- Serial.write("Servo moving with speed ");
- Serial.print(round(potentioVal) / 30);
- Serial.write(", raw potentio value:");
- Serial.print(potentioVal);
- if(timesPressed % 2 == 0){
- angleDirection = -1;
- randomNumber = random(90);
- desiredAngle = randomNumber;
- } else if (timesPressed % 2 == 1) {
- angleDirection = 1;
- randomNumber = random(90, 180);
- desiredAngle = randomNumber;
- } else {
- angleDirection = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement