I have some basic code written in PDS and would like to see about porting it over into arduino, But I have a couple of goto statmeants and I've read that the GOTO command is discouraged in C
Which would be the best way to approach this in replacing goto within the loop
here is the basic code
MAIN:
'##### CHECKS IF THE E-STOP OR FAIL SAFE IS ACTIVATED ####
If GATE_LIMIT = 0 Or MAX_HIEGHT = 0 Then
MOTOR_ENABLE = 0:DUTY=0:LIFT_DWN_COIL = 0:LIFT_UP_COIL=0:UP_FLAG = 0
EndIf
'##### WAITS FOR THE LIFT UP SWITCH TO BE PRESSED ########
If LIFT_UP = 0 Then
UP_FLAG = 1:UP_SEC = 20:MOTOR_ENABLE=1:LIFT_UP_COIL = 1
DUTY_SLOW = ERead SLOWDOWN_DUTY
DUTY = DUTY_SLOW
RSOut "LIFTING PLATFORM",13
GoTo LIFTING_UP
EndIf
'##### WAITS FOR THE LIFT DOWN SWITCH TO BE PRESSED ########
'# TURNS THE PUMP ON FOR .25 SECONDS TO PREVENT DROP ON LOWERING ##
If LIFT_DWN = 0 Then
If GATE_LIMIT = 0 Then
MOTOR_ENABLE = 0:DUTY=0:LIFT_DWN_COIL = 0:LIFT_UP_COIL=0:UP_FLAG = 0
RSOut "GATE OPEN, CLOSE GATE",13,10
GoTo MAIN
Else
MOTOR_ENABLE=1:LIFT_UP_COIL = 1
SEC_DWN = ERead DWN_SEC_TEMP
DUTY = 200
delay2 = 85
GoSub DELAYR
DWN_FLAG = 1:MOTOR_ENABLE=0:LIFT_UP_COIL = 0
LIFT_DWN_COIL = 1:RSOut "gate",Dec GATE_LIMIT
GoTo LIFTING_DOWN
EndIf
EndIf
GoTo MAIN
it jumpd here
LIFTING_UP:
'##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
If GATE_LIMIT = 0 Then
RSOut "GATE OPEN, CLOSE GATE",13,10
MOTOR_ENABLE = 0:DUTY=0:LIFT_DWN_COIL = 0:LIFT_UP_COIL=0:UP_FLAG = 0:DWN_FLAG = 0
delay2 = 100
GoSub DELAYR
GoTo MAIN
EndIf
'##### FAIL SAFE LIMIT SWITCH #########
If MAX_HIEGHT = 0 Then
RSOut "FAIL SAFE LIMIT SWITCH ACTIVATED",13
MOTOR_ENABLE = 0:DUTY=0:LIFT_DWN_COIL = 0:LIFT_UP_COIL=0:UP_FLAG = 0
delay2 = 100
GoSub DELAYR
GoTo MAIN
EndIf
DUTY_MAX1 = ERead DUTY_TEMP
DUTY_SLOW = ERead SLOWDOWN_DUTY
DelayUS 10
DUTY_MAX = DUTY_MAX1
DUTY_SLOW1= DUTY_SLOW
'#### SETS THE LIFT SPEED #################
If UP_FLAG = 1 And HEIGHT_REACHED = 1 Then
Inc DUTY
DelayUS 500
If DUTY > DUTY_MAX Then 'SETS MAX LIFT SPEED
DUTY = DUTY_MAX
EndIf
EndIf
'##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
If UP_FLAG = 1 And HEIGHT_REACHED = 0 Then
Dec DUTY
DelayMS 15
If DUTY < DUTY_SLOW1 Then
DUTY = DUTY_SLOW1
RSOut "STAGE 1 COMPLETE SLOWING DOWN",13
EndIf
EndIf
'##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
If UP_FLAG = 1 And HEIGHT_REACHED = 0 And DUTY < DUTY_SLOW1 +5 Then
LIFT_UP_COIL = 0:UP_FLAG = 0
MOTOR_ENABLE=0
RSOut "LIFTING COMPLETED",Dec DUTY,13
GoTo MAIN
EndIf
CCP1CON.4 = DUTY.0 ' Store duty to registers as
CCP1CON.5 = DUTY.1 ' a 10-bit word
CCPR1L = DUTY >> 2
'##### USED FOR DEBUGING ONLY #############
GoTo LIFTING_UP
I should be able to port all of it or most of it over to adruino has I understand most of it but just unsure about the goto part. I've got the basic part working waiting for the button presses but just turn led on/off with rather than the goto at the moment
It is frowned on because it can cause unexpected consequences, it's a bit like "beam me up Scotty" in reverse - maybe the place you GOTO is full of man-eating tigers.
The best thing would be to understand what the existing code is intended to achieve and then write some Arduino code to achieve the same thing.
I can't help feeling that if the existing code uses GOTO it probably has many other deficiencies also.
This is closer to what you want (it won't compile yet, but it is getting there):
void LIFTING_UP ()
{
while (true)
{
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
if (GATE_LIMIT == 0) {
RSOut "GATE OPEN, CLOSE GATE", 13, 10;
MOTOR_ENABLE = 0;
DUTY = 0;
LIFT_DWN_COIL = 0;
LIFT_UP_COIL = 0;
UP_FLAG = 0;
DWN_FLAG = 0;
delay2 = 100;
DELAYR ();
return;
}
// ##### FAIL SAFE LIMIT SWITCH #########
if (MAX_HIEGHT == 0) {
RSOut "FAIL SAFE LIMIT SWITCH ACTIVATED", 13;
MOTOR_ENABLE = 0;
DUTY = 0;
LIFT_DWN_COIL = 0;
LIFT_UP_COIL = 0
UP_FLAG = 0;
delay2 = 100
DELAYR ();
return;
}
DUTY_MAX1 = ERead DUTY_TEMP;
DUTY_SLOW = ERead SLOWDOWN_DUTY;
DelayUS 10;
DUTY_MAX = DUTY_MAX1;
DUTY_SLOW1 = DUTY_SLOW
// #### SETS THE LIFT SPEED #################
if (UP_FLAG == 1 && HEIGHT_REACHED == 1) {
DUTY++;
DelayUS 500;
if (DUTY > DUTY_MAX) { // SETS MAX LIFT SPEED;
DUTY = DUTY_MAX;
}
}
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
if (UP_FLAG == 1 && HEIGHT_REACHED == 0) {
DUTY--;
DelayMS 15;
if (DUTY < DUTY_SLOW1) {
DUTY = DUTY_SLOW1;
RSOut "STAGE 1 COMPLETE SLOWING DOWN", 13
}
}
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
if (UP_FLAG == 1 && HEIGHT_REACHED == 0 && DUTY < DUTY_SLOW1 + 5) {
LIFT_UP_COIL = 0;
UP_FLAG = 0;
MOTOR_ENABLE = 0;
RSOut "LIFTING COMPLETED", Dec DUTY, 13;
return;
}
CCP1CON.4 = DUTY.0 ; // Store duty to registers as
CCP1CON.5 = DUTY.1 ; // a 10-bit word
CCPR1L = DUTY >> 2;
// ##### USED FOR DEBUGING ONLY #############
} // end of while
} // end of LIFTING_UP
It looks like LIFTING_UP could be a function (which you call). When you want to return to MAIN just do a "return" which exits the function. You need to put semicolons at the end of lines (except "if") and put function arguments into brackets.
In C/C++ you generally don't use all caps for variables, for neatness you might want to avoid doing that.
Thanks Nick I'm now making progress, I now have it working but there is still lots to do which I'm going to complete a stage at a time, Lift up working now going to add lift down, The limit switch now jumps to locked where I need to add a bit more code to continue where it left off when normal state is resumed which is the next part.
Here is the code I have so far will need tidying up
const int Gate_limitpin = 11;
const int Max_hieghtlimitpin = 12;
const int Motor_enable= 3;
const int upbuttonPin = 2;
const int LIFT_DWN_COIL = 4;
const int LIFT_UP_COIL= 5;
const int HEIGHT_REACHED = 6;
boolean UP_FLAG = false;
boolean DWN_FLAG = false;
int Duty = 100;
int Duty_SLOW1 = 90;
int Duty_MAX = 150;
void setup() {
Serial.begin(9600);
pinMode(upbuttonPin, INPUT);
digitalWrite(upbuttonPin,HIGH);
pinMode(HEIGHT_REACHED,OUTPUT);
pinMode( Gate_limitpin, INPUT);
digitalWrite(Gate_limitpin,HIGH);
pinMode(Max_hieghtlimitpin, INPUT);
digitalWrite(Max_hieghtlimitpin, HIGH);
pinMode(Motor_enable, OUTPUT);
pinMode(HEIGHT_REACHED,INPUT);
digitalWrite(HEIGHT_REACHED,HIGH);
}
void loop() {
if ((digitalRead(Gate_limitpin) == LOW) || (digitalRead(Max_hieghtlimitpin) ==LOW)){
digitalWrite(Motor_enable, LOW);
Serial.println("Gate open or max hieght limit switch active");
LOCKED ();
}
if (digitalRead(upbuttonPin) == LOW){
digitalWrite(Motor_enable, HIGH);
UP_FLAG = 1;
Serial.println("lifting");
Serial.println(UP_FLAG);
LIFTING_UP ();
}
}
void LIFTING_UP ()
{
while (true)
{
Serial.print("Duty =");
Serial.println(Duty);
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES ####
if (digitalRead(Gate_limitpin) == LOW)
{
Serial.println( "GATE OPEN, CLOSE GATE");
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
digitalWrite(LIFT_UP_COIL, LOW);
digitalWrite(UP_FLAG, LOW);
digitalWrite(DWN_FLAG, LOW);
return;
}
// ##### FAIL SAFE LIMIT SWITCH #########
if (digitalRead(Max_hieghtlimitpin) == LOW) {
Serial.println( "FAIL SAFE LIMIT SWITCH ACTIVATED");
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
digitalWrite(LIFT_UP_COIL, LOW);
digitalWrite(UP_FLAG, LOW);
digitalWrite(DWN_FLAG, LOW);
return;
}
/*
Duty_MAX1 = ERead Duty_TEMP;
Duty_SLOW = ERead SLOWDOWN_Duty;
DelayUS 10;
Duty_MAX = Duty_MAX1;
Duty_SLOW1 = Duty_SLOW
*/
// #### SETS THE LIFT SPEED #################
if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == HIGH)) {
Duty++;
delay(500);
if (Duty > Duty_MAX) { // SETS MAX LIFT SPEED;
Duty = Duty_MAX;
}
}
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) == LOW)) {
Duty--;
delay(100);
if (Duty < Duty_SLOW1) {
Duty = Duty_SLOW1;
Serial.println( "STAGE 1 COMPLETE SLOWING DOWN");
}
}
// ##### CHECKING THE STATE OF THE LIMIT SWITCHES AND PRE-SLOW DOWN ACTIVATION ####
if (UP_FLAG == 1 && (digitalRead(HEIGHT_REACHED) ==LOW && Duty < Duty_SLOW1 + 5)) {
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
digitalWrite(LIFT_UP_COIL, LOW);
Serial.println( "LIFTING COMPLETED");
return;
}
} // end of while
} // end of LIFTING_UP
void LOCKED ()
{
while (true)
{
Serial.println("LIMTI SWITCH ATCIVE");
}
return;
}
I've nearly got it all working, Just a little problem with part of my code, When I push the lift down button it set the SEC_DWN timer to 15 seconds and jumps to the LIFTING_DOWN function and it counts down to zero then back to the main loop, But the trouble is because I'm using the a delay(1000); there is a 1.5 seconds delay before it jumps out of the LIFTING_DOWN. I need this to be instasnt I know this delay may be caused by the delay(1000), is the a more elegant way without using delay I've seen something about using mills but for counting up rather than down I think
Code below using delay
void LIFTING_DOWN ()
{
while (true)
{
Serial.println(SEC_DWN);
SEC_DWN --;
delay(1000);
//'######## CHECKS IF THE E-STOP HAS BEEN PUSHED ###########
if ((DWN_FLAG = 1) && (digitalRead(Gate_limitpin) == LOW)) {
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
digitalWrite(LIFT_UP_COIL, LOW);
DWN_FLAG = true;
while (digitalRead(Gate_limitpin) == LOW) {
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
digitalWrite(LIFT_UP_COIL, LOW);
Serial.println("GATE OPEN, CLOSE GATE");
}
return;
}
//####### COUNTS DOWN THE TIME AND TURNS OFF THE DOWN REALY ############
if ((DWN_FLAG == 1) && (SEC_DWN < 1 )) {
digitalWrite(Motor_enable, LOW);
digitalWrite(LIFT_DWN_COIL, LOW);
Serial.print("LOWERING COMPLETED");
if (SEC_DWN < 0)
SEC_DWN = 0;
return;
}
}
}
Wow Nick Thanks I shall be looking at that looks a lot better way of doing it than mine
Good site you have there I sure will be spending sometime on there