Count Lines

In this part of the lab, the goal is to count the amount of black lines the robot goes over for a set amount of seconds. After the time limit, the robot will then flash using the LED the amount of times the robot went over the black line. The trick of the lab was to make sure the robot isn't calculating a single black line multiple times, this was solved by only saying one black line was counted only when the floor sensor goes from HIGH to LOW

Gizmo was able to successfully count the appropriate amount of lines it went over and the results of the LED flashing were satisfactory.

The Code

void CountLines(int duration) {
   delay(2000);                                        //delay to give us time to lift our finger off the button
   Forward(0);                                         
   int lines = 0;
   int start =0;
   while((millis()-start)<duration*1000) {             //goes for duratino of seconds passed in to function.
     if (useSave) saveData(useSave);                   //logs data
     boolean currentLineState = getLineSensor();       //gets line sensor value
     if (lastLineState!=currentLineState) {            //if line sensor value changed
        if (lastLineState == false) {                  //only count lines on one edge of a change
            lines++;
        }
        lastLineState=currentLineState;                //update current line value
     }
      
   }
   Stop(2000);
   for(int i =0; i < lines; i++){                      //dispalys the number of lines on the LED
     analogWrite(ledPin, 255);
     delay(1000);
     digitalWrite(ledPin,LOW);
     delay(1000);
   }
}

A VIDEO WOULD BE GOOD HERE!