It is using the optional min and max parameters, to constrain the pulses to the 1000 to 2000 microsecond (1 to 2 millisecond) range. Pay particular attention to the attach() call on line 26. This sketch drives the servo back and forth. Be careful with shorter or longer pulses. **SparkFun code, firmware, and software is released under the MIT License().** If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.If you have not previously installed an Arduino library, please check out our installation guide. 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.Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. 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 open Arduino's Serial Montor to read the voltage output from the phototransistor board connected to A3 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. I am trying to write a code in order to have a phototransistor control a servo.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |