Hi Guys, below is the code written to operate 3 screw jack undercarriage motors, two operating from the arduino motor shield and the third from a third part device. The code has a flaw, the motors do not reverse on signal change.
#include <Servo.h>
Servo myservoA;
Servo myservoB;
//Servo myservoC;
// BOARD C CONNECTIONS
//IN1 control for motor, set LOW pin 0
//IN2 control for motor, set HIGH pin 1
//FB feedback to pin A2
//EN enable board set to high or low on pin 5 called motorCEN
//INV is motor direction set to high or low on pin 10, called motorC
//D2 is pwm motor control on pin 6
//free pins are..,1,0
int Bbrake=8;//brake signal for motor B
int Abrake=9;//brake signal for motor A
//int Cbrake=....;//brake signal for motor C.....not needed as no brake on board C
int motorspdB=11;//PWM speed signal for motor B
int motorspdA=3;//PWM speed signal for motor A
int motorspdC=6;//PWM speed signal for motor C
int motorA=12;
int motorB=13;
int motorC=10;
int motorCEN=5;//motor c board enable
int sensorPinA = A0; // select the input pin for the current
int sensorPinB = A1; // select the input pin for the current
int sensorPinC = A2; // select the input pin for the current
int sensorValueA = 0; // variable to store the value coming from the sensor
int sensorValueB = 0; // variable to store the value coming from the sensor
int sensorValueC = 0; // variable to store the value coming from the sensor
int enable=2;//signal from the receiver
//int limit=A0
void setup () {
pinMode (13, OUTPUT);//motor B enable and direction HIGH or LOW
pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
pinMode(10, OUTPUT);//motor C direction HIGH or LOW
pinMode(5, OUTPUT);//motor C enable HIGH or LOW
pinMode (8, OUTPUT);//Brake motor B
pinMode (9, OUTPUT);//Brake motor A
pinMode (2,INPUT);//receiver signal pin
pinMode (4, OUTPUT);//servo A pin
pinMode (7, OUTPUT);//servo B pin
pinMode (0, OUTPUT);
pinMode (1, OUTPUT);
pinMode(motorspdC, OUTPUT);
digitalWrite(motorCEN, HIGH);
//pinMode(..., OUTPUT);servo C pin//no servo on motor C
//pinMode (6, OUTPUT);//speed signal pin for motor C(not sure this is required)
myservoA.attach(4);
myservoB.attach(7);
Serial.begin(9600);
//myservoC.attach(...)need to decide on which pin;
}
void loop () {
int limitA = 200; //this is the current limit for motor A
int limitB = 200; //this is the current limit for motor B
int limitC = 60; //this is the current limit for motor C
//boolean switchstate = false;
// boolean switchstate2 = false;
int switchstate;
int switchstate2;
sensorValueB = analogRead(sensorPinB); //this reads the value of current for motorB and sets the variable sensorValueB
sensorValueA = analogRead(sensorPinA); //this reads the value of current for motorA and sets the variable sensorValueA
sensorValueC = analogRead(sensorPinC); //this reads the value of current for motorC and sets the variable sensorValueC
//int updwn = pulseIn(enable, INPUT); //this sets the value of the variable updwn which is read from transmitter switch
//int updwn2 = pulseIn(enable, INPUT); //this sets the comparison value of updwn which is read from transmitter switch
int switchread = pulseIn(enable,INPUT);
if (switchread > 14435) {
switchstate = 1;
}
else {
switchstate = 0;
}
//int updwncalc = sq(updwn - updwn2);
//int updwncalc = (updwn-updwn2);
//int updwncalc2 = (updwncalc*updwncalc);
int iB = 0; //this creates and sets a variable named iB which we will use to make sure the motors stop once they reach overcurrent condition
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
int iC = 0; //this creates and sets a variable named iC which we will use to make sure the motors stop once they reach overcurrent condition
myservoA.writeMicroseconds(900);
myservoB.writeMicroseconds(900);
// myservoC.writeMicroseconds(900);
switchstate2 = switchstate;
delay(300);
while (switchstate2 == switchstate) { //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 (switchstate = 1 && sensorValueB < limitB && iB==0) {
analogWrite (motorspdB,255);
digitalWrite (motorB,HIGH);
digitalWrite (Bbrake,LOW);
// Serial.print(switchstate);
}
else if (switchstate = 0 && sensorValueB < limitB && iB==0) {
analogWrite (motorspdB,255);
digitalWrite (motorB,LOW);
digitalWrite (Bbrake,LOW);
}
else {
analogWrite (motorspdB,0);
digitalWrite (Bbrake,LOW);
myservoB.writeMicroseconds(2095);
iB++;
}
delay(300);
if (switchstate = 1 && sensorValueA < limitA && iA==0) {
analogWrite (motorspdA,255);
digitalWrite (motorA,HIGH);
digitalWrite (Abrake,LOW);
}
else if (switchstate = 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(100);
if (switchstate = 1 && sensorValueC < limitC && iC==0) {
//digitalWrite(motorCEN, HIGH); // enable motor board **is this the correct way of creating a digital output signal, is this 5v??
//should this not be on throughout the loop?
analogWrite (motorspdC,255);
digitalWrite (motorC,HIGH);//determins direction of motor C
//digitalWrite (Cbrake,LOW); Not required on motor C
}
else if (switchstate = 0 && sensorValueC < limitC && iC==0) {
//digitalWrite(motorCEN, HIGH); // enable motor board
digitalWrite (motorC,LOW);
analogWrite (motorspdC,255);
//digitalWrite (Cbrake,LOW);//no brake on c motor
}
else {
analogWrite (motorspdC,0);
//digitalWrite (Cbrake,LOW);//no brake on c motor
//digitalWrite(motorCEN, LOW); // disable motor board
//myservoC.writeMicroseconds(2095);//no servo on C motor
iC++;
}
delay (200);
sensorValueB = analogRead(sensorPinB);
sensorValueA = analogRead(sensorPinA);
sensorValueC = analogRead(sensorPinC);
// updwn2 = pulseIn(enable, INPUT);
// updwncalc = sq(updwn -updwn2);
// Serial.print("switchstate2");
switchread = pulseIn(enable, INPUT);
Serial.print(switchread);
if (switchread < 14435) {
//Serial.print(switchread);
switchstate2 = 1;
//Serial.print("switchstate2");
//switchstate2 = false;
}
else {
switchstate2 = 0;
//Serial.print(switchstate2);
// switchstate2 = true;
}
}
}
Any help greatly appreciated