Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Similar to last Summer my dad, Joel Fajans asked me to write some simple arduino code for him. This code is designed to take two sets of for inputs and only output a signal if all these inputs match a user defined configuration.
/* Abe Zukor 14/08/2016
* this program takes about 60 microseconds per loop
* If this program is not uploading to the arduino try opining the arduino IDE from the exe file instead of from the .ino file.
*
*/
//These are all anyone should ever need to change
//set these to 0 (off) 1 (on) or 2 (null/ dont care) the order is {Overld, Highlp, Active, Status). These vaules are for group 1. group 1 is on the left.
int pins1State[] = {0, 0, 2, 1};
//set these to 0 (off) 1 (on) or 2 (null/ dont care) the order is {Overld, Highlp, Active, Status). These vaules are for group 2. group 2 is on the right.
int pins2State[] = {0, 0, 2, 1};
/*
You should not need to change anything below this point in the program.
*/
//these are just program wide constants
//these bits correspond to what is on the board
// this is group 1
int pins1[] = {1, 3, 2, 0};
int pinOut1 = 4;
//group 2
int pins2[] = {7, 9, 8, 6};
int pinOut2 = 5;
//int loopnumber = 0;
void setup() {
//sets all the inputs
pinMode(pins1[0], INPUT);
pinMode(pins1[1], INPUT);
pinMode(pins1[2], INPUT);
pinMode(pins1[3], INPUT);
pinMode(pins2[0], INPUT);
pinMode(pins2[1], INPUT);
pinMode(pins2[2], INPUT);
pinMode(pins2[3], INPUT);
//sets the outputs
pinMode(pinOut1, OUTPUT);
pinMode(pinOut2,OUTPUT);
}
void loop() {
//this is the main program loop
//Serial.println(micros());
// put your main code here, to run repeatedly:
//writes to the pins if pinscorrect retrurns true
//Serial.println("group 1");
digitalWrite(pinOut1, pinsCorrect(pins1, pins1State));
//Serial.println("group 2");
digitalWrite(pinOut2, pinsCorrect(pins2, pins2State));
//for timing the loop
/*if (loopnumber==10000){
Serial.println(micros());
loopnumber = 0;
}
loopnumber++;*/
}
boolean pinsCorrect(int pins[], int states[]){
//This function determins whether or not the inputs into the arduino match the desired pattern
//initialises some variables
boolean pinstate;
boolean allcorrect = true;
//cycles through the arrays checking if the current value matches the desired value
for (int arraynumber = 0; arraynumber<4; arraynumber++){
pinstate = !digitalRead(pins[arraynumber]);
/*
//uncomment this for debugging pourposes
Serial.println(pins[arraynumber]);
Serial.println(pinstate);
Serial.println(states[arraynumber]);*/
//if not set the functions output to false unless that pin is set to 2 or dont care
if ((int)pinstate!=states[arraynumber]&&(states[arraynumber]!=2)){
allcorrect = false;
}
}
return allcorrect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment