Switching Motors

My code is supposed to allow me to press a button to move the motor at pin 9 but when I go and press the button the motor at pin 5 activates and turns the wrong motor. I am trying to let the motor in pin 5 do a sweep (45 degrees) stop, and then allow the button to activate the motor at pin 9 to do a 90-degree turn. Can anyone help? Code below

#include <Servo.h>
const int BUTTON_PIN = 7; //Arduino pin connected to button’s pin
const int SERVO_PIN = 9; //Arduino pin connected to servo motor’s pin
const int SERVO_PIN2 = 5;
Servo servo; //Create servo object to control a servo
int angle = 0; //the current angle of servo motor
int angle2 = 0;
int lastButtonState; //the previous state of button
int currentButtonState; //the current state of button

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
int pirPin = 3; //the digital pin connected to the PIR sensor’s output
int ledPin = 13;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int calibrationTime = 30; //the time when the sensor outputs a low impulse long unsigned
int lowIn; //the amount of milliseconds the sensor has to be low

void setup() {
// put your setup code here, to run once:
{
Serial.begin(9600); // We’ll send debugging information via the Serial monitor
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW); //give the sensor some time to calibrate
Serial.print(“Calibrating Sensor “);
for (int i = 0; i < calibrationTime; i++){
Serial.print(”.”);
delay(1000);
}
Serial.println(" done");
Serial.println(“Sensor Active”);
delay(50);
}
pinMode(BUTTON_PIN, INPUT_PULLUP); //set arduino pin to input pull-up mode
servo.attach(SERVO_PIN); //attaches the servo on pin 9 to the servo object
servo.attach(SERVO_PIN2);
servo.write(angle);
servo.write(angle2);
currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
// put your main code here, to run repeatedly:

{
photocellReading = analogRead(photocellPin);
Serial.print(“Analog reading = “);
Serial.print(photocellReading); // the raw analog reading
if (photocellReading < 10) {
Serial.println(” - Dark”);
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
delay(5000);
}
if(photocellReading < 500){
if (digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){ //makes sure we wait for a transition to LOW before any further output is made
lockLow = false;
Serial.println("—");
Serial.print(“motion detected at “);
Serial.print(millis()/1000);
Serial.println(” sec”);
delay(50);
} takeLowTime = true;
} if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); // save the time of the transition from HIGH to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
} //if the sensor is low for more than the given pause, we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){ //makes sure this block of code is only executed again after a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at “); //output
Serial.print((millis()-pause)/1000);
Serial.println(” sec ");
delay(50);
}
}
if(digitalRead(pirPin) == HIGH){
Serial.println(“Swinging arm in motion”);
for(angle2 = 0; angle2 < 180; angle2++){
if(angle2 == 179){
break;
}
}
if(digitalRead(pirPin) == HIGH){
Serial.println(“The Button is active”);
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println(“The button is pressed”);
if(angle == 0) // change angle of servo motor
angle = 90;
else
if(angle == 90)
angle = 0;
servo.write(angle);// control servo motor arccoding to the angle

}
}
}

{

}
}
}

Your coding style looks good. :slight_smile:

In the Arduino IDE, use CTRL T to format your code.

Use the </> button from the ‘reply menu’ to attach the copied sketch.

Servo servo; //Create servo object to control a servo
Servo servo2; //Create 2nd servo object to control a servo ←
. . . .
servo.attach(SERVO_PIN); //attaches the servo on pin 9 to the servo object
servo.attach(SERVO_PIN2); // ???

You want 2 servos in your code don’t you ? :wink:

servo.attach(SERVO_PIN); //attaches the servo on pin 9 to the servo object
servo2.attach(SERVO_PIN2); //attaches the servo on pin 5 to the servo object ←

Thank you for responding, this is my new code and I am currently testing to see if your suggestions worked.

#include <Servo.h>
const int BUTTON_PIN = 7; //Arduino pin connected to button’s pin
const int SERVO_PIN = 9; //Arduino pin connected to servo motor’s pin
const int SERVO_PIN2 = 5;
Servo servo; //Create servo object to control a servo
Servo servo2;
int angle = 0; //the current angle of servo motor
int angle2 = 0;
int lastButtonState; //the previous state of button
int currentButtonState; //the current state of button

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
int pirPin = 3; //the digital pin connected to the PIR sensor’s output
int ledPin = 13;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int calibrationTime = 30; //the time when the sensor outputs a low impulse long unsigned
int lowIn; //the amount of milliseconds the sensor has to be low

void setup() {
// put your setup code here, to run once:
{
Serial.begin(9600); // We’ll send debugging information via the Serial monitor
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW); //give the sensor some time to calibrate
Serial.print(“Calibrating Sensor “);
for (int i = 0; i < calibrationTime; i++) {
Serial.print(”.”);
delay(1000);
}
Serial.println(" done");
Serial.println(“Sensor Active”);
delay(50);
}
pinMode(BUTTON_PIN, INPUT_PULLUP); //set arduino pin to input pull-up mode
servo.attach(SERVO_PIN); //attaches the servo on pin 9 to the servo object
servo2.attach(SERVO_PIN2);
servo.write(angle);
servo.write(angle2);
currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
// put your main code here, to run repeatedly:

{
photocellReading = analogRead(photocellPin);
Serial.print(“Analog reading = “);
Serial.print(photocellReading); // the raw analog reading
if (photocellReading < 10) {
Serial.println(” - Dark”);
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
delay(5000);
}
if (photocellReading < 500) {
if (digitalRead(pirPin) == HIGH) {
Serial.println(“Swinging arm in motion”);
for (angle2 = 0; angle2 < 180; angle2++) {
servo2.write(angle2);
break;
}
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if (lockLow) { //makes sure we wait for a transition to LOW before any further output is made
lockLow = false;
Serial.println("—");
Serial.print(“motion detected at “);
Serial.print(millis() / 1000);
Serial.println(” sec”);
delay(50);
} takeLowTime = true;
} if (digitalRead(pirPin) == LOW) {
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if (takeLowTime) {
lowIn = millis(); // save the time of the transition from HIGH to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
} //if the sensor is low for more than the given pause, we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) { //makes sure this block of code is only executed again after a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at “); //output
Serial.print((millis() - pause) / 1000);
Serial.println(” sec ");
delay(50);
}
}
if (digitalRead(pirPin) == HIGH) {
Serial.println(“The Button is active”);
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println(“The button is pressed”);
if (angle == 0) // change angle of servo motor
angle = 90;
else if (angle == 90)
angle = 0;
servo.write(angle);// control servo motor arccoding to the angle

  }
}

}

{

}
}

Hello
I guess you have to clean up your sketch wrt the salat of brackets:

  }
}

}

{

}
}

:slight_smile:

Just did it lol


pinMode(BUTTON_PIN, INPUT_PULLUP); //set arduino pin to input pull-up mode
servo.attach(SERVO_PIN); //attaches the servo on pin 9 to the servo object
servo2.attach(SERVO_PIN2);
servo.write(angle);
servo.write(angle2);  // did you mean       servo2.write(angle2);   ?????
currentButtonState = digitalRead(BUTTON_PIN);
}



for (angle2 = 0; angle2 < 180; angle2++) {
servo2.write(angle2);
break;   //   having a break doesn’t make any sense  ????
}

Please
In the Arduino IDE, use CTRL T to format your code.
Use the </> button from the ‘reply menu’ to attach the copied sketch.


What is this supposed to do ?

for(angle2 = 0; angle2 < 180; angle2++){
if(angle2 == 179){
break;
}
}

Did you want to add a servo write in here ?
And if you do, add some time to move the servo too.

And what’s with the ‘break’ you are only going to 179 already :woozy_face:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.