Week 12. Nov 5.9 2018 - michelle-qin/Portfolio GitHub Wiki

Got started on the Zoetrope project. Working on fixing an issue: When the motor is turned off, we want the light to turn off and the potentiometer to reset to 0.

Pulled the existing files from GitHub. Reached an error when compiled - arduino couldn't find the Flexy library. Mr. Harlow helped me fix that issue after a lot of trouble-shooting.

Now, I'm working on the first part of the project: when the motor is turned off, we want the light to turn off. Before doing this, I went through the code and implemented comments so I could understand what each function was doing. I focused specifically on this function, which was written by previous students:

void updateBeamState() {
  beamState = !digitalRead(BEAM_BUTTON);  

if (beamState != lastBeamState && beamState == HIGH) {   //if beamState is HIGH; beamState is beam button on/off
  beamCount = !beamCount;   //make beamCount TRUE

if (beamCount == false) {      //if beamCount is FALSE, make beam button light & LEDs LOW
  digitalWrite(BEAM_BUTTON_LIGHT, LOW);
  digitalWrite(BEAM_LEDS, LOW);
  Serial.println("[debug] updateBeamState() off");
}
else {    //if beamCount is TRUE, make beam button light & LEDs HIGH
  digitalWrite(BEAM_BUTTON_LIGHT, HIGH);
  digitalWrite(BEAM_LEDS, HIGH);
  Serial.println("[debug] updateBeamState() on");
}

} lastBeamState = beamState; //stepper.processMovement(); }

I separated the if and else blocks and made them into separate functions named turnOnBeam() and turnOffBeam(). See below:

void updateBeamState() {
  beamState = !digitalRead(BEAM_BUTTON);  

if (beamState != lastBeamState && beamState == HIGH) {  
  beamCount = !beamCount;   //make beamCount TRUE

if (beamCount == false) {      //if beamCount is FALSE, make beam button light & LEDs LOW
  turnOffBeam();
}
else {    //if beamCount is TRUE, make beam button light & LEDs HIGH
  turnOnBeam();
}

} lastBeamState = beamState; //stepper.processMovement(); } void turnOffBeam() { digitalWrite(BEAM_BUTTON_LIGHT, LOW); digitalWrite(BEAM_LEDS, LOW); Serial.println("[debug] updateBeamState() off"); } void turnOnBeam () { digitalWrite(BEAM_BUTTON_LIGHT, HIGH); digitalWrite(BEAM_LEDS, HIGH); Serial.println("[debug] updateBeamState() on"); }

Then, in loop(), I added the following:

if (motorCount == false) {
  turnOffBeam();
  beamState = LOW;
  beamCount = false;

This way, whenever the motor is turned off, the beam is turned off.