Hey all,
I want to make a system with servo, arduino and relay module, but I have a trouble making it. I use 2 buttons to control a servo, one button turns servo 180 degrees and other turns it back. All it works perfectly separatly with this code:
#define B1 2 //Button 1
#define B2 6 //Button 2
#define RELAY1 7
#define RELAY2 8
Servo servoA;
int position;
int sensorVal = 1;
int sensorVal1 = 1;
void setup() {
servoA.attach(9);
Serial.begin(9600);
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
sensorVal = digitalRead(B1);
sensorVal1 = digitalRead(B2);
//print out the value of the pushbutton
Serial.println(sensorVal);
Serial.println(sensorVal1);
if (sensorVal == LOW && position < 170) {
servoA.write(position++);
delay(5);
}
if (sensorVal1 == LOW && position >= 3) {
servoA.write(position--);
delay(5);
}
Serial.println(sensorVal);
Serial.println(sensorVal1);
Relays separatly also work perfectly but when I add this part of code servo stops working at all:
I dont even connect relays to scheme actually but servo doesnt work because of these lines. Servo doesnt work with simple leds if I connect them to arduino by this code but leds or relays are working.
I found what when I add delay function after digitalWrite it stops work.
I cant imagine how to fix that, I hope you could help me.
You haven't posted anything we can test - ie the whole sketch that doesn't work. The one that works
doesn't need fixing and the snippet from the one that doesn't is not complete so cannot be tested!
Post the entire sketch, explaining what it should do and what it is actually doing. Describe all
the hardware you have connected too, where that is needed to run the test.
MarkT:
You haven't posted anything we can test - ie the whole sketch that doesn't work. The one that works
doesn't need fixing and the snippet from the one that doesn't is not complete so cannot be tested!
Post the entire sketch, explaining what it should do and what it is actually doing. Describe all
the hardware you have connected too, where that is needed to run the test.
That sketch which I have posted and it is my all sketch, but I add it one more time:
#define B1 2 //Button 1
#define B2 6 //Button 2
#define LED1 7
#define LED2 8
Servo servoA;
int position;
int sensorVal = 1;
int sensorVal1 = 1;
void setup() {
servoA.attach(9);
Serial.begin(9600);
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
sensorVal = digitalRead(B1);
sensorVal1 = digitalRead(B2);
//print out the value of the pushbutton
Serial.println(sensorVal);
Serial.println(sensorVal1);
if (sensorVal == LOW && position < 170) {
servoA.write(position++);
delay(5);
}
if (sensorVal1 == LOW && position >= 3) {
servoA.write(position--);
delay(5);
}
Serial.println(sensorVal);
Serial.println(sensorVal1);
digitalWrite(LED1, LOW);
delay(2000);
digitalWrite(LED1, HIGH);
delay(2000);
digitalWrite(LED2, LOW);
delay(3000);
digitalWrite(LED2, HIGH);
delay(3000);
I want to control my servo by two buttons, that is working so far by the first part of sketch, and I want to add to the same sketch e.g some leds turning on and turning off after some time interval, but when my servo stops working when I add digitalWrite and delay to the sketch.
Hardware scheme is very simple, I just add two buttons which I connect to 2 and 6 pins, servo motor which I'm connecting to 9 pin and powering it with external power source and add some leds to 7 and 8 pins. (I've changed relays into leds because it is much simpler).
I don't see any obvious fault in your program. But all those long delays may give the impression that it is not working because there will be a total of 11 seconds of inactivity in every iteration of loop(). If you press a button before that period has expired it won't be detected. And there are probably only a few microseconds in which it will be detected. If this seems to be the problem try holding a button down for longer than 11 seconds.
Have a look at how millis() is used to manage timing without blocking in several things at a time