Skip to content

Instantly share code, notes, and snippets.

@cgobat
Created April 14, 2020 02:52
Show Gist options
  • Select an option

  • Save cgobat/ebd78fae8258e5f3c4f428970a3b2487 to your computer and use it in GitHub Desktop.

Select an option

Save cgobat/ebd78fae8258e5f3c4f428970a3b2487 to your computer and use it in GitHub Desktop.
Science Olympiad Electric Vehicle code. Target distance is set using 3 rotary dials and progress is counted using a hall effect sensor.
long cm; //goal distance
long target; //target # of pulses
volatile long pulses; //current # of pulses
long timer; //time elapsed since start
float conv = 10.208; //cm per 100 pulses
long gatecm = 900; //distance to run before slowing down
long gateTarget; //number of pulses the previous translates to
long dir; //current direction of motion
void setup()
{
pinMode(4, INPUT_PULLUP); // SW1 bit 0
pinMode(5, INPUT_PULLUP); // SW1 bit 1
pinMode(6, INPUT_PULLUP); // SW1 bit 2
pinMode(7, INPUT_PULLUP); // SW1 bit 3
pinMode(15, INPUT_PULLUP); // SW2 bit 0
pinMode(14, INPUT_PULLUP); // SW2 bit 1
pinMode(8, INPUT_PULLUP); // SW2 bit 2
pinMode(9, INPUT_PULLUP); // SW2 bit 3
pinMode(19, INPUT_PULLUP); // SW3 bit 0
pinMode(18, INPUT_PULLUP); // SW3 bit 1
pinMode(17, INPUT_PULLUP); // SW3 bit 2
pinMode(16, INPUT_PULLUP); // SW3 bit 3
pinMode(2, INPUT_PULLUP); // encoder
pinMode(13, INPUT_PULLUP); // switch
attachInterrupt(0, encoderPinChange, CHANGE);
Serial.begin(115200);
}
void loop() {
int hun, ten, one, val, i;
// The rotary switch bits are pulled up when open.
// When closed, they connect to ground, so an "active"
// bit reads 0/low
hun = 0;
hun += digitalRead(4) == 0 ? 100 : 0;
hun += digitalRead(5) == 0 ? 200 : 0;
hun += digitalRead(6) == 0 ? 400 : 0;
hun += digitalRead(7) == 0 ? 800 : 0;
ten = 0;
ten += digitalRead(15) == 0 ? 10 : 0;
ten += digitalRead(14) == 0 ? 20 : 0;
ten += digitalRead(8) == 0 ? 40 : 0;
ten += digitalRead(9) == 0 ? 80 : 0;
one = 0;
one += digitalRead(19) == 0 ? 1 : 0;
one += digitalRead(18) == 0 ? 2 : 0;
one += digitalRead(17) == 0 ? 4 : 0;
one += digitalRead(16) == 0 ? 8 : 0;
cm = hun + ten + one; //read target value from dials
target = (cm * 100L) / conv; //calculate needed pulses until target
gateTarget = (gatecm * 100L) / conv; //calculate needed pulses until gate
while (digitalRead(13) == 1); //wait for switch to be pressed
delay(10); //wait to avoid bouncing
pulses = 0; //reset encoder
timer = millis(); //set time out count
dir = 1;
analogWrite(10, 230);
analogWrite(11, 0);
while (pulses <= gateTarget && digitalRead(13) == 0 && millis() <= timer + 10000); //encoder count to 9 meters or time out if never reached
analogWrite(10, 0);
analogWrite(11, 0);
delay(10); //wait to dissipate field
analogWrite(10,0);
analogWrite(11, 230);
delay(1000);
analogWrite(10, 0);
analogWrite(11, 100); //turn on brakes
delay(2000); //wait to finish coast
analogWrite(11, 0);
analogWrite(11, 50); //turn on brakes
delay(2000); //wait to finish coast
if (pulses < target) { //if we have yet to reach it,
timer = millis(); //reset time out baseline
dir = 1;
analogWrite(10, 120); //motor creep in original direction
analogWrite(11, 0); //motor still off in other direction
while (pulses < target && digitalRead(13) == 0 && millis() <= timer + 10000); //encoder count to target
analogWrite(11, 0);
analogWrite(10, 0);
delay(10); //wait to dissipate EM field
analogWrite(10, 0);
analogWrite(11, 30); //turn on brakes
delay(3000); //wait to finish coast
}
analogWrite(10, 0); //turn off
analogWrite(11, 0); //motor completely
while (digitalRead(13) == 0) //wait for switch to be flipped back up
Serial.println(pulses);
}
void encoderPinChange() {
pulses += dir;
}
@cgobat
Copy link
Copy Markdown
Author

cgobat commented Apr 19, 2020

ev_design.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment