Page 345 - Making things move_ DIY mechanisms for inventors, hobbyists, and artists
P. 345
322 Making Things Move
{
up( distance );
totalHeight += distance; //increment totalHeight variable
}
else if ((lowest == photoValues[1]) && ((totalHeight - distance) > 0))
{
down( distance );
totalHeight -= distance; //decrement totalHeight variable
}
else if ((lowest == photoValues[2]) && ((totalWidth - distance) > 0))
{
left( distance );
totalWidth -= distance; //decrement totalWidth variable
}
else if ((lowest == photoValues[3]) && ((totalWidth + distance) <
CANVASWIDTH))
{
right( distance );
totalWidth += distance; //increment totalWidth variable
}
}//end if
//otherwise, no one is covering any sensors, draw according to random
else
{
//pick random number 1 through 9 to map to direction
randomDirection = random(1, 9);
Serial.print("random direction:");
Serial.println(randomDirection);
//pick random number 1 through 200 to map to distance
randomDistance = random(1, 200);
Serial.print("random distance:");
Serial.println(randomDistance);
//directions for any randomDirection value generated
switch (randomDirection)
{
case 1: //go up
if((totalHeight + randomDistance) < CANVASHEIGHT)
{
up(randomDistance);
totalHeight += randomDistance;
}
break;