Skip to content

Instantly share code, notes, and snippets.

@cmrichards
Last active December 23, 2015 11:09
Show Gist options
  • Save cmrichards/6626773 to your computer and use it in GitHub Desktop.
Save cmrichards/6626773 to your computer and use it in GitHub Desktop.
// Just for a laugh I have built 2 more possible versions of the code.
// There is so little code that either way is probably as good as the other,
// but if there were more code then the changes would mean more.
int analogueIn = A0; //input pin for pressure pad
int digitalOut1 = 8; //output pin for motor control
int sensorIn; //variable for triggering the statement in while loop
int primer; //variable for triggering the primer switch
int primerS = A2; //input for primer switch
int digitalOut2 = 4; //output pin for trigger ready LED
// Original Code
void loop ()
{
primer = analogRead(primerS);
sensorIn = analogRead(analogueIn);
if (sensorIn > 400 && primer >=600) //when the sensor is triggered
{
digitalWrite(digitalOut2, LOW);
digitalWrite(digitalOut1, HIGH);
delay(500); //value to be tested and TBC
digitalWrite(digitalOut1, LOW);
delay(3000); //value to be tested and TBC
}
else if (sensorIn < 400 && primer >=600) //when the sensor is primed
{
digitalWrite(digitalOut2, HIGH);
delay (5);
}
else if (primer < 600) //when the sensor is not primed
{
digitalWrite(digitalOut2, LOW);
delay (5);
}
}
// Possible version 1.
// Here I have Restructured the IF statements and removed some of the conditions.
// I noticed that you duplicated the condition 'primer >=600' in two of
// your IF statements, so I extracted it out. Now there are only 2 conditions
// in the code, 'primer >=600' and 'sensorIn > 400', rather than 5 conditions in
// the original code.
void loop ()
{
primer = analogRead(primerS);
sensorIn = analogRead(analogueIn);
if (primer >=600) // sensor is primed
{
if (sensorIn > 400) //when the sensor is triggered
{
digitalWrite(digitalOut2, LOW);
digitalWrite(digitalOut1, HIGH);
delay(500); //value to be tested and TBC
digitalWrite(digitalOut1, LOW);
delay(3000); //value to be tested and TBC
}
else
{
digitalWrite(digitalOut2, HIGH);
delay (5);
}
} else {
//when the sensor is not primed
digitalWrite(digitalOut2, LOW);
delay (5);
}
}
// Possible version 2.
// This is probably the version I would use, but like I said, all versions
// would be fine because there is so little code.
// Here I use the 'return' statement which will return from the function and
// not run the rest of the code.
// A good technique when writing functions is to return quickly if none of the
// basic conditions required for the primary purpose of the function are met.
// The primary purpose of this code is to act as a feeder for an animal by reacting
// to a pressure pad and opening a feeder, so
// if the primer on/off switch is not on then we should just quickly exit the function.
// The main body of the code (the bit that will grow as you add more functionality)
// should be separate from the primer on/off switch so that this main code is less complex.
// I hope I explained that well enough.
void loop ()
{
primer = analogRead(primerS);
sensorIn = analogRead(analogueIn);
if (primer < 600)
{
// Turn off the light if the primer is not on and leave the function.
digitalWrite(digitalOut2, LOW);
delay (5);
return;
}
// Main body/purpose of function is seperate from the on/off switch code above :
if (sensorIn > 400) //when the sensor is triggered
{
digitalWrite(digitalOut2, LOW);
digitalWrite(digitalOut1, HIGH);
delay(500); //value to be tested and TBC
digitalWrite(digitalOut1, LOW);
delay(3000); //value to be tested and TBC
}
else
{
digitalWrite(digitalOut2, HIGH);
delay (5);
}
}
// Here is a more extreme refactor that I wouldn't do at this stage, it's overkill, but would
// be worth it if it was going to get a lot more complex.
// Notice that the main loop function is now instantly understandable because of the function names, which
// is important if you're going to be maintaining complex code.
void loop ()
{
if (!isActive()) { turnLightOff(); return; }
if (pressurePadIsTriggered())
{
turnLightOff();
openTheGate();
delay(3000);
} else {
turnLightOn();
}
}
boolean isActive()
{
// primer switch is on.
return analogRead(primer) >= 600;
}
void turnLightOff()
{
digitalWrite(digitalOut2, LOW); // turn/keep light off
delay (5);
}
boolean pressurePadIsTriggered()
{
return sensorIn > 400;
}
void openTheGate()
{
// turn on the motor for just 0.5 second
digitalWrite(digitalOut1, HIGH);
delay(500);
digitalWrite(digitalOut1, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment