Could those with a bit of time please go through my sketch and give me some guidance as to what i could do better and where things could be a bit more streamline.
Given that I have only been writing code for two weeks without a single class, feel free to go easy on me...
I hope a photo of the breadboard layout will suffice
//Joystick Inputs:
const int updownInPin = A4; // Analog input pin that "up down stick" is attached to
const int leftrightInPin = A5; // Analog input pin that the "left right stick" is attached to
const int estopInPin = A2; // Activate and deactivate the "E-Stop"
const int lockoutInPin = A3; // Activate and deactivate the "E-Stop"
const int faultInPin = 12; // Reset any Faults on the drive may encounter
//Signal Outputs to Vairiable Speed Drives:
const int upOutPin = 6; // UP LED or Clock Wise for drive A
const int downOutPin = 10; // DOWN LED or Counter Clock Wise for drive A
const int leftOutPin = 11; // LEFT LED or Clock Wise for drive B
const int rightOutPin = 9; // RIGHT LED or Counter Clock Wise for drive B
const int faultOutPin = 4; // Fault Reset
const int estopOutPin = 2; // Emergency Stop
//PWM Outputs to Drives:
const int updownOutPin = 5; // Analog input pin that "Up Down JStick" is attached to
const int leftrightOutPin = 3; // Analog input pin that "Left Right JStick" is attached to
//for information only
const int centreOutPin = 7;
int updownJSValue = 0; // value read from the "up down Joy Stick"
int leftrightJSValue = 0; // value read from the "left right Joy Stick"
int updownoutputValue = 0; // value output to the PWM (analog out)
int leftrightoutputValue = 0; // value output to the PWM (analog out)
int estopState = 0;
int lockoutState = 0;
int faultState = 0;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, INPUT_PULLUP);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
}
void loop() {
//Fault reset:
faultState = digitalRead (faultInPin);
if (faultState == LOW){
digitalWrite (faultOutPin, HIGH);
}
else {
digitalWrite (faultOutPin, LOW);
}
// E-Stop and push botton Lock Out Feature
estopState = digitalRead (estopInPin);
lockoutState = digitalRead (lockoutInPin);
if (estopState == HIGH && lockoutState == HIGH){
analogWrite (estopOutPin, LOW);
updownJSValue = analogRead(updownInPin);
leftrightJSValue = analogRead(leftrightInPin);
//Signal for UP to go HIGH:
if (updownJSValue >=10 && updownJSValue <=508){
digitalWrite (upOutPin, HIGH);
updownoutputValue = map(updownJSValue, 507, 100, 0, 255);
analogWrite (updownOutPin, updownoutputValue);
}
else{
digitalWrite (upOutPin, LOW);
}
//Signal for LEFT to go HIGH:
if (leftrightJSValue >=10 && leftrightJSValue <=508){
digitalWrite (leftOutPin, HIGH);
leftrightoutputValue = map(leftrightJSValue, 507, 100, 0, 255);
analogWrite (leftrightOutPin, leftrightoutputValue);
}
else{
digitalWrite (leftOutPin, LOW);
}
//Signal for DOWN to go HIGH:
if (updownJSValue >=517 && updownJSValue <1013){
digitalWrite (downOutPin, HIGH);
updownoutputValue = map(updownJSValue, 518, 900, 0, 255);
analogWrite (updownOutPin, updownoutputValue);
}
else{
digitalWrite (downOutPin, LOW);
}
//Signal for RIGHT to go HIGH:
if (leftrightJSValue >=517 && leftrightJSValue <=1013){
digitalWrite (rightOutPin, HIGH);
leftrightoutputValue = map(leftrightJSValue, 508, 900, 0, 255);
analogWrite (leftrightOutPin, leftrightoutputValue);
}
else{
digitalWrite (rightOutPin, LOW);
}
//Indidcation Joystick is in Centre Position
if (updownJSValue >508 && updownJSValue <517) {
digitalWrite (centreOutPin, HIGH);
}
else if (leftrightJSValue >508 && leftrightJSValue <517) {
digitalWrite (centreOutPin, HIGH);
}
else{
digitalWrite (centreOutPin, LOW);
}
}
else {
analogWrite (updownOutPin, 0);
analogWrite (leftrightOutPin, 0);
digitalWrite (estopOutPin, HIGH);
digitalWrite (centreOutPin, HIGH);
digitalWrite (upOutPin, LOW);
digitalWrite (downOutPin, LOW);
digitalWrite (leftOutPin, LOW);
digitalWrite (rightOutPin, LOW);
}
Serial.print(" U D PWM = " );
Serial.println(updownoutputValue);
Serial.print("U D JoyS = " );
Serial.println(updownJSValue);
Serial.print(" L R PWM = " );
Serial.println(leftrightoutputValue);
Serial.print("L R JoyS = " );
Serial.println(leftrightJSValue);
Serial.print(" E-Stop = " );
Serial.println(estopState);
Serial.print("Lock Out = " );
Serial.println(lockoutState);
Serial.println();
delay(10);
}