Page 336 - Making things move_ DIY mechanisms for inventors, hobbyists, and artists
P. 336
Chapter 10 Projects 313
//assign actual photocell value to "lowest" variable if it's lower
//than whatever "lowest" is set to (starts at 9999)
if (lowest >= photoValues[i] )
{
lowest = photoValues[i];
}
//print it out to confirm that the lowest value is being selected
Serial.print("lowest:");
Serial.println(lowest);
delay(1000); //wait one second before looping so we can read the values
}//end for
distance = lowest; //set travel distance variable = lowest value
//find the sensor that matched the lowest, go that direction
//see below for what the up, down, left, right functions do
if (lowest == photoValues[0])
{
up( distance );
}
else if (lowest == photoValues[1])
{
down( distance );
}
else if (lowest == photoValues[2])
{
left( distance );
}
else if (lowest == photoValues[3])
{
right( distance );
}
}//end loop
/*
Here are the directional functions. Loop size = distance.
Positive step numbers are clockwise, negative counterclockwise
*/
void up(int distance) {
for(i=0;i< distance; i++){
right_motor.step(10);
left_motor.step(-10);
}
}