Hello everyone! I apologize in advance if I am breaking any forum rules or standards. I am a complete beginner. I am reaching out on behalf of my group. We have been assigned a robotics task in school, and we have zero background in robotics.
One circuit we have intends to move the motors 180 degrees when the button is pressed. Then, it should return to 0 degrees when the button is pressed again. The code works perfectly fine in Tinkercad, and it works okay in real life. However, the button appears to send inconsistent bursts of information to the Arduino Uno microcontroller. The motors sometimes move twice after just one button press.
From what I've seen on the forums so far, its something about Arduino not being able to check the state changes perfectly. The code we use has INPUT_PULLUP to help with that, but the real life circuit still has issues. I vaguely remember another forum post saying that a capacitor can help, but we do not have a capacitor. If there is any code that can somehow simulate how a capacitor works, the same way INPUT_PULLUP can simulate a pullup resistor, we would appreciate it if it can be shared with us.
Our real life circuit is using a 9 volts battery. I'm not sure if that is important information. If there is any other information needed, I will be glad to supply it. Any help is appreciated. I can also send the code we use, and full transparency, we took that code from a website and just slightly repurposed it. Please send any resources or links that may help. Thank you so much. Our project hinges on you all. The deadline of the project is November 13. We are sorry. We love you. From the Philippines.
Is it a PP3 battery by any chance ?
If so then it is only intended to power low current devices such as smoke detectors. Have you measured its voltage whilst powering your project ?
Please post your full sketch, using code tags when you do
Hello! Thank you for your response. I will be sure to try those button libraries. I will update this thread once I find something that works (or if I have any other questions...)
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-servo-motor
*/
#include <Servo.h>
// constants won't change
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int SERVO_PIN1 = 9; // Arduino pin connected to servo motor's pin
const int SERVO_PIN2 = 10;
Servo servo1; // create servo object to control a servo
Servo servo2;
// variables will change:
int angle = 0; // the current angle of servo motor
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
servo1.attach(SERVO_PIN1); // attaches the servo on pin 9 to the servo object
servo2.attach(SERVO_PIN2);
servo1.write(0);
servo2.write(0);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo1.write(angle);
servo2.write(angle);
}
}
The schematic shows that the button pin (2) and motor pin (10) are shorted together. Please, isolate it and run your sketch again and report thr result.
I followed a servo motor and pushbutton connection online. Should I remove it? I have tried removing it already in place of a jumperwire instead, but it retains the same problem, wherein jittering occurs to one of the motors, while as the other ceases completely when connected to a 9V Battery. It works perfectly fine when connected to a laptop or PC.
The photographs are not too useful. Your post # 6 is a copy of your instructions. Trustworthy wiring diagrams are drawn by hand while CAREFULL verifying where each end of each wire is conected. Post a picture of that. Meanwhile you should have picked up on the fact one 9VDC battery can not work, either get a wal wart or a big powerbank or a bench supply.