Monday, October 12, 2015

Mini project 1 process

Mini project 1 process

  With this project, I wanted to morph previous projects we had done and cannibalize the code to create something that can work in unison. The two projects I am going to dissect is, Blink with the LEDs and Sweep with the servo motor.

In the video below, I took the code from the Blink project but instead of using the delay() I used milis() for the timing of the blink. I did this instead because with the delay() you cant respond to inputs or change outputs.


Instead of relying on delay() to time the blinking. I used a state machine which remembers the current state of the LED and the last time it changed. On each pass through the loop, it looks at the millis() clock to see if it is time to change the state of the LED again. This shortens the amount of code you have to apply to the arduino program and makes it easier For each LED and Servo added I added another state machine. Using separate state machines allows us to blink the two LEDs and move the motors completely independent of one another.  

LEDs and Servo motors with state machines.

For the final piece, I made a class for the flashers and the motors and for every LED that I wanted to flash, I created an instance of the Flasher class by calling the constructor.

 
This makes the code shorter and easier to read. And, since there is no duplicated code, it also compiles smaller. That leaves even more precious memory to do other things with the arduino. So every extra LED or Servo I want to add, it only requires two extra lines of code. Each LED and Motor work independently of each other.

No comments:

Post a Comment