@John re:Code review 1
@John re: Code review 1
Hi John,
When a pin is defined as INPUT_PULLUP, an internal pullup resistor to +5 Volts is attached to the pin, so a normally open SPST switch attached to GND and the pin, will be HIGH until the switch is closed and the pin reads LOW.
In setup() these 2 lines are not needed:
digitalWrite ( KickbackApproach_pin, HIGH); // Intialize to a known state, magnet not at KickbackApproach reedswitch
digitalWrite ( KickbackDivert_pin, HIGH); // Intialize to a known state, magnet not at KickbackDivert reedswitch
The question regarding the following lines is " Are you really trying to leave the Motor ON (MotorRelay_pin = LOW) UNTIL ChainHome_pin goes LOW?"
If yes, then the following statement does not do what you want
if (digitalRead (ChainHome_pin == LOW));
digitalWrite( MotorRelay_pin, HIGH); // end of setup-turn off motor
If I gues correctly, then what you want is:
while ((digitalRead (ChainHome_pin) == HIGH) // NOTE RE-ARRANGEMENT OF PARENTHESIS
digitalWrite( MotorRelay_pin, LOW); // Keep looping with Motor On Until Chain is HOME
digitalWrite( MotorRelay_pin, HIGH); // Now we're Home and out of loop, so turn off motor
Also note the statement: if (digitalRead (ChainHome_pin) == LOW);
Actually does nothing since you terminated it with the ";" before the next statement which is normally executed when the "if" test is TRUE.
In loop() one does not initialize inputs -- they are set by the external reed switches, and setup() does any one time init. If you need to wait for them to be at a known state, then you need to do other things so they sense the conditions you want. Further, by putting these in loop() they are not only redundant, they are executed over and over, rapidly, at the beginning of each loop iteration-- I suspect not what you want.
//specify state of Inputs/Outputs at start of Loop
digitalWrite ( CageServo_pin, HIGH); // Intialize to a known state, cage alligned with track
digitalWrite ( KickbackServo_pin, HIGH); // Intialize to a known state, track set to Divert
digitalWrite ( ChainHome_pin, LOW); // Intialize to a known state, magnet at ChainHome
digitalWrite ( SkipArrive_pin, HIGH); // Intialize to a known state, awaiting skip arrivalt
digitalWrite ( MotorRelay_pin, HIGH); // Intialize to a known state, motor off
digitalWrite ( Cage_pin, HIGH); // Intialize to a known state, magnet not at CAGE reedswitch
digitalWrite ( KickbackApproach_pin, HIGH); // Intialize to a known state, magnet not at KickbackApproach reedswitch
digitalWrite ( KickbackDivert_pin, HIGH); // Intialize to a known state, magnet not at KickbackDivert reedswitch
I think you need to get rid of all the extra ";" again: CHANGE THE FOLLOWING AND NOTE ";" PLACEMENT AND CHANGE OF PARENTHESIS WHICH WILL CORRECT SOME LOGIC ERRORS:
if (digitalRead ((ChainHome_pin) == LOW) && digitalRead (( SkipArrive_pin) == LOW)) ;
digitalWrite (MotorRelay_pin, LOW); // start motor
//condition when magnet moves away from ChainHome
if (digitalRead (ChainHome_pin) == HIGH && digitalRead ( SkipArrive_pin) == LOW) ;
(digitalWrite (MotorRelay_pin, LOW)); // run motor
//condition after magnet picks up skip
if (digitalRead ((ChainHome_pin) == HIGH) && digitalRead (( SkipArrive_pin) == HIGH)) ;
digitalWrite (MotorRelay_pin, LOW); // run motor
//Case 2 - Tip skip at CAGE
// Start tip - Stop chain and set CageServo_pin LOW to start tipping cycle
if (digitalRead ( Cage_pin == LOW )); //magnet on chain arrives at CAGE reedswitch
digitalWrite( MotorRelay_pin, HIGH); // stop motor, prepare to tip skip
digitalWrite ( CageServo_pin, LOW); // Move cage to tipped position, will take 3.5 seconds to get there
//hold cage at Tip position for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion plus a smidge
//return cage to Home position aligned with track
digitalWrite( CageServo_pin, HIGH); // Move Cage back to Home position aligned with track, will take 3.5 seconds to get there
//wait to ensure skip and cage are aligned with track for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion plus a smidge
//disable tip circuit - Set CAGE HIGH
digitalWrite(Cage_pin, HIGH); // over-ride CAGE readswitch to prevent re-triggering
//start chain
digitalWrite(MotorRelay_pin, LOW); // start motor, haul skip to kickback
//Case 3 - Kickback points operation
//empty skip approaches kickback point blades
//set to straight path with KickbackApproach
if (digitalRead (KickbackApproach_pin == LOW)); //magnet on chain arrived at KickbackApproach reedswitch
digitalWrite ( KickbackServo_pin, LOW); // Set track to Straight
//remain set and allow skip to pass Kickback point blades
//set to divert path with KickbackDivert
if (digitalRead (KickbackDivert_pin == LOW)); //magnet on chain arrived at KickbackDivert reedswitch
digitalWrite ( KickbackServo_pin, HIGH); // Set track to Divert
//wait with motor running until magnet reaches ChainHome reedswitch
//Case 4 - Chain magnet runs back to ChainHome after releasing skip
//stop chain when magnet gets to ChainHome
if (digitalRead(ChainHome_pin) == LOW);
digitalWrite(MotorRelay_pin, HIGH); // turn off motor
//Wait until Case 1, when next skip arrives from the incline.
CHANGE TO:
//Case 1 - Motor start and moves skip to CAGE when skip triggers SkipArrive reedswitch;
// Skip arrives at SkipArrive, start chain by MotorRelay;
if ((digitalRead (ChainHome_pin) == LOW) && (digitalRead ( SkipArrive_pin) == LOW))
digitalWrite (MotorRelay_pin, LOW); // start motor
//condition when magnet moves away from ChainHome
if ((digitalRead (ChainHome_pin) == HIGH) && (digitalRead ( SkipArrive_pin) == LOW))
digitalWrite (MotorRelay_pin, LOW); // run motor
//condition after magnet picks up skip
if ((digitalRead (ChainHome_pin) == HIGH) && (digitalRead ( SkipArrive_pin) == HIGH))
digitalWrite (MotorRelay_pin, LOW); // run motor
//Case 2 - Tip skip at CAGE
// Start tip - Stop chain and set CageServo_pin LOW to start tipping cycle
if (digitalRead ( Cage_pin) == LOW ) //magnet on chain arrives at CAGE reedswitch
digitalWrite( MotorRelay_pin, HIGH); // stop motor, prepare to tip skip
digitalWrite ( CageServo_pin, LOW); // Move cage to tipped position, will take 3.5 seconds to get there
//hold cage at Tip position for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion plus a smidge
//return cage to Home position aligned with track
digitalWrite( CageServo_pin, HIGH); // Move Cage back to Home position aligned with track, will take 3.5 seconds to get there
//wait to ensure skip and cage are aligned with track for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion plus a smidge
//disable tip circuit - Set CAGE HIGH
digitalWrite(Cage_pin, HIGH); // over-ride CAGE readswitch to prevent re-triggering
//start chain
digitalWrite(MotorRelay_pin, LOW); // start motor, haul skip to kickback
//Case 3 - Kickback points operation
//empty skip approaches kickback point blades
//set to straight path with KickbackApproach
if (digitalRead (KickbackApproach_pin) == LOW) //magnet on chain arrived at KickbackApproach reedswitch
digitalWrite ( KickbackServo_pin, LOW); // Set track to Straight
//remain set and allow skip to pass Kickback point blades
//set to divert path with KickbackDivert
if (digitalRead (KickbackDivert_pin) == LOW) //magnet on chain arrived at KickbackDivert reedswitch
digitalWrite ( KickbackServo_pin, HIGH); // Set track to Divert
//wait with motor running until magnet reaches ChainHome reedswitch
//Case 4 - Chain magnet runs back to ChainHome after releasing skip
//stop chain when magnet gets to ChainHome
if (digitalRead(ChainHome_pin) == LOW)
digitalWrite(MotorRelay_pin, HIGH); // turn off motor
//Wait until Case 1, when next skip arrives from the incline.
Again John, look at the way the while statement is structured in setup() as I suggested and examine whether you need to use this in any places above in the loop( ). I suspect this will be necessary. Remember that if statements are tested ONCE and then they fall through to the next statement.
What you want to happen is that your code starts an operation and waits for it to conclude or waits until the next sensor indicates you are ready for the next action. The little Arduino is looking at those switches more than a million times faster than you are reading a line of code! ...and moving on to the next statement quickly. So you need to tell it to wait for an event, or slow it down like you did with the delay(4000); statements so your mechanism can catch up to your code! I hope this makes a bit of sense. Ask more questions as needed.
I think you have made much progress. Have fun!

Best regards,
Geoff