I'm trying to run 2 servos and 6 motors off one power supply (1 servo 3 motors controlled each arduino).
However, I'm having trouble with the servos jittering.
I've tried adding a large capacitor across the power supply, which didnt fully solve the issues, and I tried adding a small capacitor across the control of the servos and ground, but that stopped them from working.
I've attached the code below in full, but I can explain components if required.
The motors are controlled by Mosfets, and the servos by buttons.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
Servo servo;
unsigned long rtime;
int paddlemotorControl = 8;
int turnmotorControl = 9;
int endmotorControl = 10;
unsigned long timer = 0;
unsigned long bolttimer = 0;
const byte buttonPin = 11;
byte buttonState = LOW;
byte lastButtonState = LOW;
boolean servoFlag = false;
const int buttonaddPin = 12;
int buttonaddState = 0;
int totalcounter = 0;
int tcounter = 0; //total bolts
int cinput = 0;
int coutput = 0;
int clive = 0;
int prevcinput = 0;
int cbatcher = 0;
int dclive = 0;
int dtcounter = 0;
int currentState = 0; //For the final light gate
int previousState = 0;
int currentState2 = 0; //For the initial light gate
int previousState2 = 0;
int sensorPin = A0; //For the final light gate
int sensorValue = 0;
int sensorPin2 = A1; //For the initial light gate
int sensorValue2 = 0;
int load = 0;
int starter = 0;
int paddlestarter = 0;
int turnstarter = 0;
int var = 0;
int batch = 0;
boolean stopper = false;
int presenceState = 0;
int c1 = 0;
int c2 = 0;
int cs = 0;
int ps = 0;
int dps1 = 0;
int dps2 = 0;
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
servo.attach(2);
pinMode(buttonPin, INPUT);
pinMode(buttonaddPin,INPUT);
pinMode(paddlemotorControl, OUTPUT);
pinMode(turnmotorControl, OUTPUT);
pinMode(endmotorControl, OUTPUT);
Serial.begin(9600);
servo.write(0);
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
}
void loop(){
rtime = millis();
tcounter = totalcounter - coutput;
clive = cinput - coutput;
Serial.println("c");
Serial.println(sensorValue);
Serial.println(sensorValue2);
Serial.println("t");
Serial.println();
Serial.println();
Serial.println("s");
Serial.println();
Serial.println();
if (stopper == false){
Serial.println("Connected!");
}else{
Serial.println("Failed!");}
if(starter == 0){ //ONE
digitalWrite(endmotorControl, HIGH);
}
if(paddlestarter == 0){
digitalWrite(paddlemotorControl, HIGH);
}
if(turnstarter == 0){
digitalWrite(turnmotorControl, HIGH);
}
//total bolts = tcounter, add button to add (10) to tcounter - like bc scanner. counter2 = #live bolts
lcd.setCursor(0,0); // TWO
lcd.print("RGS Cash Guys");
lcd.setCursor(0,1);
lcd.print("Bolt Distribution");
lcd.setCursor(0,2);
lcd.print("Qashqai =");
lcd.setCursor(4,3);
lcd.print(" bolts are live");
if(clive < 10){
lcd.setCursor(0,3);
lcd.print(clive);
lcd.setCursor(1,3);
lcd.print(" ");
}
if(clive >= 10){
if(clive <= 100){
lcd.setCursor(0,3);
lcd.print(clive);
lcd.setCursor(2,3);
lcd.print(" "); }}
if(clive >= 100){
lcd.setCursor(0,3);
lcd.print(clive);
lcd.setCursor(3,3);
lcd.print(" ");}
if(tcounter < 10){
lcd.setCursor(10,2);
lcd.print(tcounter);
lcd.setCursor(11,2);
lcd.print(" ");}
if(tcounter >= 10){
if(tcounter <= 100){
lcd.setCursor(10,2);
lcd.print(tcounter);
lcd.setCursor(12,2);
lcd.print(" "); }}
if(tcounter >= 100){
lcd.setCursor(10,2);
lcd.print(tcounter);
lcd.setCursor(13,2);
lcd.print(" "); }
buttonState = digitalRead(buttonPin);
buttonaddState = digitalRead(buttonaddPin);
if(buttonaddState == HIGH){//to add bolts, count 10 and add, then press button to sync with counter system
cs = 1; }else{
cs = 0;
ps = 0;}
if(cs != ps) {
if(cs == 1){
totalcounter = totalcounter + 10;
ps = 1;
}}
sensorValue2 = analogRead(sensorPin2); // read input value of 1st light gate
if (sensorValue2 >= 900) {
currentState2 = 1;} else {
currentState2 = 0;}
if(currentState2 != previousState2){
if(currentState2 == 1){
cinput = cinput + 1;}}
previousState2 = currentState2;
sensorValue = analogRead(sensorPin); //read sensor value of 2nd light gate
if (sensorValue >= 000) {
currentState = 1;} else {
currentState = 0;}
if(currentState != previousState){
if(currentState == 1){
coutput = coutput + 1;}}
previousState = currentState;
if(cinput != prevcinput){ // THREE
paddlestarter = 1;
digitalWrite(paddlemotorControl, LOW);
stopper = true;
}
prevcinput = cinput;
if(clive >= 8){ //FOUR
turnstarter = 1;
digitalWrite(turnmotorControl, LOW);}
if(coutput - 4 == cbatcher){ ///FIVE
starter = 1;
digitalWrite(endmotorControl, LOW);
batch = batch + 1;
cbatcher = coutput;
load = 1;
}
if(load == 1){ //SIX
if (servoFlag == false && lastButtonState != buttonState)
{
lastButtonState = buttonState;
if (buttonState == HIGH)
{
servo.write(60);
Serial.println("servo at 60");
timer = rtime;
servoFlag = true;
}
}}
if (servoFlag == true && rtime - timer >= 7500)
{
servo.write(0);
Serial.println("servo at 0");
load = 0;
starter = 0;
servoFlag = false;
}
if(batch >= 2){ //SEVEN
turnstarter = 0;
batch = 0;}
if((stopper == true) && (clive < 5)){ //EIGHT
presenceState = cinput;
stopper = false;
bolttimer = rtime;} else if(clive >= 5){
paddlestarter = 1;
digitalWrite(paddlemotorControl, LOW);
}
if(presenceState == cinput && bolttimer <= rtime - 5000){
paddlestarter = 0;
stopper = false;
bolttimer = 0;
presenceState = 0;
} else { if(bolttimer <= rtime - 5000){
bolttimer = 0;
}}
}
What could be going wrong?
Thanks.
p.s. yeah there are probs a lot of unnecessary variables, but im shite at code lol