Can anyone help me with this code. The serial print at the end of the code does not print. Which makes me assume the program is sticking before the end. this would make sense as the motor and servo do not operate.
#include <Servo.h>
#define midlimit 1700
Servo myservoA;
int Abrake = 9;//brake signal for motor A
int motorspdA = 3;//PWM speed signal for motor A
int ch5;
int motorA = 12;
int sensorPinA = A0; // select the input pin for the current
int switchpin = 22;
int sensorValueA = 0; // variable to store the value coming from the sensor
void setup () {
pinMode (44, INPUT);//signal from the RC reciever channel 5
pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
pinMode (9, OUTPUT);//Brake motor A
pinMode (4, OUTPUT);//servo A pin
pinMode (switchpin, OUTPUT);
myservoA.attach(4);
}
void loop () {
Serial.begin(9600);
int limitA = 300; //this is the current limit for motor A
sensorValueA = analogRead(sensorPinA); //this reads the value of current for motorA and sets
// the variable sensorValueA
int iA = 0; //this creates and sets a variable named iA which we will use to make sure the
//motors stop once they reach overcurrent condition
myservoA.writeMicroseconds(900);
ch5 = pulseIn(44, HIGH);
Serial.println(ch5);
if (ch5 < midlimit) {
digitalWrite(switchpin, HIGH);
}
else {
digitalWrite(switchpin, LOW);
}
int updwn = digitalRead(switchpin); //this sets the value of the variable updwn which is
// read from transmitter switch
int updwn2 = digitalRead(switchpin); //this sets the comparison value of updwn which is
// read from transmitter switch
Serial.println(updwn);
delay(300);
while (updwn == updwn2) { //the while loop here knows the current state of the switch
// updwn. during the while loop it will continually check
//the state of the switch by setting updwn2 therefore once the state of the switch
//changes it will exit the while loop
if (updwn==1 && sensorValueA < limitA && iA==0) {
analogWrite (motorspdA,255);
digitalWrite (motorA,HIGH);
digitalWrite (Abrake,LOW);
}
else if (updwn==0 && sensorValueA < limitA && iA==0) {
analogWrite (motorspdA,255);
digitalWrite (motorA,LOW);
digitalWrite (Abrake,LOW);
}
else {
analogWrite (motorspdA,0);
digitalWrite (Abrake,LOW);
myservoA.writeMicroseconds(2125);
iA++;
}
}
delay (200);
sensorValueA = analogRead(sensorPinA);
int ch5 = pulseIn(44, HIGH);
if (ch5 < midlimit) {
digitalWrite(switchpin, HIGH);
}
else {
digitalWrite(switchpin, LOW);
}
updwn2 = digitalRead(switchpin);
Serial.println(updwn2);
}