If you use VarSpeedServo.h instead of Servo.h you can just change Servo.myservo to VarSpeedServo.myservo then myservo.write (pos) to myservo.write (pos, speed) where speed can vary from 1 (very very slow) to 255 (fast). It just sweeps the servo to and fro.Ĭan you tell from this code that the servo is moving in response to the argument passed to the write method? Perhaps you should use the value you read from the analog pin to calculate a position to write the servo to. slipstick: Or there is an alternative servo library which allows you to set the speed. This is the only part that moves the servo: for(pos = 0 pos =1 pos-=1) // goes from 180 degrees to 0 degreesĪnd nowhere in it is the value from that analogRead used at all. You also have threshold set to 3, so barely anything on the phototransistor trips this if. This is the only part that bothers with the reading from the analog pin, and it is writing digital to the servo pin, that isn't going to help the servo move in any normal way. Return float(analogRead(adPin)) * 5.0 / 1024.0 Myservo.write(pos) // tell servo to go to position in variable 'pos'ĭelay(15) // waits 15ms for the servo to reach the positionįloat volts(int adPin) // Measures volts at adPin if the analog value is high enough, turn on the servo:ĭelay(1) // delay in between reads for stabilityįor(pos = 0 pos =1 pos-=1) // goes from 180 degrees to 0 degrees read the value of the phototransistor: Serial.println(" volts") // Display voltage and newline Serial.print(volts(A3)) // Display measured voltage Serial.print("A3 = ") // Display values for input A3 Myservo.attach(9) // attaches the servo on pin 9 to the servo object initialize the servo pin as an output: Int pos = 0 // variable to store the servo positionĬonst int analogPin = A3 // pin that the sensor (phototransistor) is attached toĬonst int servoPin = 9 // pin that the servo is attached toĬonst int threshold = 3 // an arbitrary threshold level that's in the range of the analog input a maximum of eight servo objects can be created Servo myservo // create servo object to control a servo There was a problem preparing your codespace, please try again. Contribute to arduino-libraries/Servo development by creating an account on GitHub. open Arduino's Serial Montor to read the voltage output from the phototransistor board connected to A3 Contribute to arduino-libraries/Servo development by creating an account on GitHub. use the appropriate driver board and be sure to ground it to the Arduino! serial port: choose the appropriate one depending on which USB port your device is connected to board: select the appropriate Arduino or Fabduino option Input_phototransistor_controlling_servo.ino (2.72 KB) I am not sure what I am doing wrong, but since I know the hardware works, I feel that I must somehow not be using the "if" statement correctly, despite having looked at examples. When I open the serial monitor, I can see that the phototransistor is indeed working and sending out different voltage values related to light levels. The servo runs, but the phototransistor had no effect on the result. I can compile and upload the code without any errors, however, it is not yet doing what I want it to. Now I am trying to get these two codes to work together using an “if” statement so that the light levels control the turning of the servo I have successful used an example code from online to get the phototransistor to work and display corresponding voltage levels in the serial monitor I have successfully used the "sweep" example from the arduino program's built-in examples to control a servo I have tried to cut and paste from a few different code examples to make one that does this. Myservo.I am trying to write a code in order to have a phototransistor control a servo. They can never be as good as the real thing. (I tried the code in Arduino simulator) - What happens with a real Arduino and a real servo Simulators are merely an approximation. Int pos = 0 // variable to store the servo position 1 when I swtich 180 degrees with 90 witch is what I need, my servo doesn't do that - So what does it do. twelve servo objects can be created on most boards
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |