Currently working on a project using a few DC motors, a Servo and an IR sensor.
I am trying to get a Servo to be controlled via IR sensor once I reach a certain destination. The IR sensor needs to tell the Servo to move a desired position once it sees a hole in a rotating disk and the voltage is lower than a specified amount. As of now the code I have works, but not exactly how I want it. The servo does not immediately react when the hole passes the IR sensor and that is what I need.
Any help would be much appreciated!
The following is the code I have at the moment, and the bolded portion is the area I sure the error is.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Servo.h>
Servo Servo9;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' , M3
Adafruit_DCMotor *Motor1 = AFMS.getMotor(3);
// Attach the switch to pin 3 - make it a constant so it can't be changed
const int motor_switch = 3;
// Selct which 'port' motor goes in
Adafruit_DCMotor *Motor2 = AFMS.getMotor(1);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor Running via Switch Input");
AFMS.begin(); // create with the default frequency 1.6KHz
//configure the motor_switch as an input and enable the internal pull-up resistor
pinMode(motor_switch, INPUT_PULLUP);
// Set the speed to maximum
// The speed won't change, so we can define it in setup
Motor1->setSpeed(150);
// Set the speed to maximum
// The speed won't change, so we can define it in setup
Motor2->setSpeed(200);
Serial.begin(9600);
Servo9.attach(9);
Servo9.write(175);
}
void loop() {
// While the button is held down, run the motor
while(!digitalRead(motor_switch))
Serial.println("Button Pressed. Running motor...");
// Set the direction of the motor to FORWARD
// The direction won't change, so we can define it in setup
Motor1->run(FORWARD);
// Delay for 4350 ms before checking the swtich again
delay(4790);
// if the button is not pressed, stop the motor
Motor1->run(RELEASE); // Stop the motor
// Delay for 5000 ms before checking the swtich again
delay(4000);
{
int value=analogRead(0);
// print out the raw value that was read:
Serial.print("The raw analog reading was ");
Serial.println(value);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = value * (5.0 / 1023.0);
Serial.print("This corresponds to an analog votlage of ");
Serial.print(voltage);
Serial.println("V");
{ if (voltage > 1.5){
** Serial.println("servo closed position.");**
** Servo9.write(175);**
** delay(100);**
}
** else if (voltage < 1.5) {**
** Serial.println("servo open position.");**
** Servo9.write(50);**
}
{
// Set the direction of the motor to BACKWARDS
Motor1->run(BACKWARD);
// Delay for 1000 ms before checking switch again
delay(1155);
// if the button is not pressed, stop motor
Motor1->run(RELEASE); // Stop motor
// Delay for 1000 ms before checking switch again
delay(1000);
// Motor 1 activated, dropping beacon cage down
Motor2->run(BACKWARD);
// Motor 2 drop delay
delay(3610);
// if the button is not pressed, stop motor
Motor2->run(RELEASE); // Stop motor
// Delay for 1000 ms then wait until data center is reached
delay(1000);
// Continue direction of the motor BACKWARDS
Motor1->run(BACKWARD);
// Delay for 3000 ms before checking switch again
delay(2430);
// if the button is not pressed, stop motor
Motor1->run(RELEASE); // Stop motor
// Delay for 5000 ms before checking switch again
delay(2000);
// Motor 2 activated, lifts cage at data center
Motor2->run(FORWARD);
// Delay cage lift
delay(3610);
// Motor 2 function ends completely
Motor2->run(RELEASE); // Stop motor
// Delay time between end motor 2 and beginning of motor 1 movement
delay(1000);
// Continue direction of motor BACKWARDS
Motor1->run(BACKWARD);
// Delay for 1500 ms before checking switch again
delay(1170);
// if the button is not pressed, stop motor
Motor1->run(RELEASE); // Stop motor
// Delay for 10000 ms before checking switch again
delay(10000);
}}} }