Question
Combining both my servo motor code and my encoder code in arduino. Example so when the angle of my encoder is between 40-80 degrees it
Combining both my servo motor code and my encoder code in arduino. Example so when the angle of my encoder is between 40-80 degrees it jumps to the servo motor code and runs. If it gets outside of that range servo motor stops.
//code must include Servo.h library in order to work //wanted servo library for testing of motor
//Servo //orange pin9 //brown gnd //red 5v
//Led //pwm8
//Encoder //red power 5v //black ground //green pin 2 //white pin 3
#include
Servo myservo; //for project encoder has 2 outputs volatile unsigned int encoderPosition = 0; //This variable will increase or decrease depending on the rotation of encoder float angle = 0.0; int angle2= 0.0;
void setup() { Serial.begin (9600); //start location
//inputs pinMode(2, INPUT); // sets pin 2 to an input pinMode(3, INPUT); // sets pin 3 to an input //outputs pinMode(8, OUTPUT); //sets pin 8 to an input
digitalWrite(2, HIGH); // sets pin 2 to a high input digitalWrite(3, HIGH); // sets pin 3 to a high input digitalWrite(8, LOW); //Sets pin 8 to a low input
//Setting up interrupt attachInterrupt(0, encA, RISING); //on pulse of encoderA input with the rising edge PWM 2 attachInterrupt(1, encB, RISING); ////on pulse of encoderB input with the rising edge PWM 3
}
void loop(){ Serial.println (encoderPosition, DEC); angle = encoderPosition * (6.0/20.0); //????according to math though should be (3.0/20.0) though requires two rotations equatls 360 degg Serial.println (angle); //displays angle of encoder in degrees }
//For first outputA void encA(){
//Channel A going from low to high edge
if (digitalRead(2) == HIGH) { // check channel B to see which way encoder is turning if (digitalRead(3) == LOW) { encoderPosition = encoderPosition + 1; //Turning clockwise } else { encoderPosition = encoderPosition - 1; //Turning counter clockwise } } else // must be a high-to-low edge on channel A {
// check channel B to see which way encoder is turning if (digitalRead(3) == HIGH) { encoderPosition = encoderPosition + 1; //encoder truning clockwise } else { encoderPosition = encoderPosition - 1; //encoder turning coutner clockwise } } }
//For second output B void encB(){
//Channel B going from low to high edge if (digitalRead(3) == HIGH) { // check channel A to see which way encoder is turning if (digitalRead(2) == HIGH) { encoderPosition = encoderPosition + 1; //encoder truning clockwise } else { encoderPosition = encoderPosition - 1; //encoder counter truning clockwise } } // Look for a high-to-low on channel B else { // check channel B to see which way encoder is turning if (digitalRead(2) == LOW) { encoderPosition = encoderPosition + 1; //encoder turuning clockwise } else { encoderPosition = encoderPosition - 1; //encoder counter truning clockwise } } }
void servoloop(){
//angle = constrain(angle, 0, 80); //angle = constrain(angle, 100, 140); //angle = constrain(angle, 160, 200); //angle = constrain(angle, 220, 2600); //angle = constrain(angle, 280, 320); //angle = constrain(angle, 340, 360);
for (angle2=0; angle2<120; angle2+=1) //goes from 0 to 120 degrees in steps of 1 degree { myservo.write(angle2); //directs servo to go to position angle delay(20); //waits 20ms between servo commands }
digitalWrite(8, HIGH); //Sets pin 8 to a high input delay(5000); digitalWrite(8, LOW); //Sets pin 8 to a low input
for (angle2=120; angle2>=1; angle2-=1) //goes from 120 to 0 degrees { myservo.write(angle2); //moves servo back in opposite direction delay(20); //pwm 20ms } delay(10000); //wait for 10 seconds
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started