I have Nano board connected to 2 servos, 1 KY-008 laser, 1 LED and 1 selector switch. Schematic in attachment. My problem that input D11 always stays HIGH, D13 reacts together with internal LED. And if I will take out button from circuit together with IF condition, then servo going back and forward some amount of time, then D13 with internal LED comes HIGH and board going in reboot. This code I wrote as test for my circuit.
Program code:
#include <Servo.h>
Servo axis1;
Servo axis2;
const int laserPin = 12;
const int ledPin = 13;
const int inCycleTooglePin = 11;
const int servo1Pin = 5;
const int servo2Pin = 6;
int internalCounter = 0;
bool isInCycle = false;
bool internalLed = false;
int axis1Pos = 0;
bool axis1Direction = false;
int axis2Pos = 0;
int pos = 0;
void setup() {
pinMode(laserPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(inCycleTooglePin, INPUT);
axis1.attach(servo2Pin);
axis2.attach(servo1Pin);
}
void loop() {
digitalWrite(laserPin, HIGH);
internalCounter++;
if (digitalRead(inCycleTooglePin) == HIGH)
{
if (internalCounter > 10)
{
if (internalLed)
{
digitalWrite(ledPin, HIGH);
internalLed = false;
}
else
{
digitalWrite(ledPin, LOW);
internalLed = true;
}
internalCounter = 0;
}
for (pos = 40; pos <= 140; pos += 1) {
axis1.write(pos);
delay(15);
}
for (pos = 140; pos >= 40; pos -= 1) {
axis1.write(pos);
delay(15);
}
}
delay(100);
}
So it is either wired something differently from your diagram or switch SW1 is broken. Have you measured the voltage on D11? Dose it change with the switch position?
Driving one servo from the internal 5V is pushing it, driving two is pushing it over the top. At least try some decoupling.
Grumpy_Mike:
So it is either wired something differently from your diagram or switch SW1 is broken. Have you measured the voltage on D11? Dose it change with the switch position?
Driving one servo from the internal 5V is pushing it, driving two is pushing it over the top. At least try some decoupling.
What happens when the servos are removed?
I measured switch with attached board with NO power, switch works as should. When I powered up the board, then D11 has 4.7V all the time. I thought that I have short, but checked the board and found nothing. As you see in test program I run only 1 servo and laser, second servo in idle mode, but power supplied to the servo.
When you said that I need to decouple , did you mention decouple power only or power and signal together?
How many pins does your button have? Simple push buttons typically have 2 pins for each side of the switch, which are always connected. Try turn the switch by 90°.
DrDiettrich:
How many pins does your button have? Simple push buttons typically have 2 pins for each side of the switch, which are always connected. Try turn the switch by 90°.
My toggle switch is 2 position, 3 pins. 1 common, rest 2 for each position.