Page 344 - Making things move_ DIY mechanisms for inventors, hobbyists, and artists
P. 344

Chapter 10    Projects     321




                       photo_right = analogRead(3);
                       Serial.print("right");
                       Serial.println(photo_right);

                       delay(1000); //give me time to read them in the monitor
                       //before drawing, check our totalHeight and totalWidth
                       Serial.print("totalHeight:");
                       Serial.println(totalHeight);
                       Serial.print("totaWidth:");
                       Serial.println(totalWidth);

                       delay(1000);  //give me time to read them in the monitor
                       //store photocell values in an array
                       int photoValues[]= {photo_up, photo_down, photo_left, photo_right};
                       lowest = 9999; //set this higher than possible photocell values

                       //loop to find lowest photocell value
                       for(i = 0;i<4; i++) //4 = number of sensors
                         {
                         Serial.println(photoValues[i]);  //prints out photoValue array
                         //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 = lowest value

                       //if lowest value indicates a covered photocell, draw towards lowest
                       if (lowest < 550 )
                         {
                         //find the sensor that matched the lowest, go that direction,
                         //but only if SADbot is within the bounds of the canvas
                         if ((lowest == photoValues[0]) && ((totalHeight + distance) <
                     CANVASHEIGHT))
   339   340   341   342   343   344   345   346   347   348   349