Tuesday, February 14, 2017

Rotating the motor when one sensor value is greater than the other

The following code should be added to the code from the previous post in order to first read the output values from each of the four photoresistors and then, depending on which photoresistor has the greatest output, rotate the motor. This code rotates the motor in one direction if sensor 3 or 4 is greater and then rotates the motor in the opposite direction if 1 or 2 is greater.

-------------------------------------------------------------------------------------------------------------------------------------
//If photoresistor 1 or 2 is greater, spin motor one way and if 3 or 4 is greater, spin motor opposite way
  if (greatestValue==3 || greatestValue==4){   
    if(pos<180){ 
      pos = ++pos;
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      //delay(15); // waits 15ms for the servo to reach the position
     Serial.print("\t");
     //Serial.print(pos);
    }
    else{
      pos = 180;
      Serial.print("\t");
      //Serial.print(pos);
   }}

 
  if (greatestValue==1 || greatestValue==2){  
     if(pos>0){
      pos = --pos;
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      //delay(15);                       // waits 15ms for the servo to reach the position
      Serial.print("\t");
      //Serial.print(pos);
     }
     else{
      pos=0;
      Serial.print("\t");
      //Serial.print(pos);
   }}
 
  delay(200);        // delay in between reads for stability

--------------------------------------------------------------------------------------------------------------------------


I have also come up with a design that allows me to use the servo motor that only turns 180 degrees. I will build a cardboard disk the same size as the lazy susan with rubber around the circumference, and mount it on top of the motor. This will then be pressed up against the lazy susan so as the motor/cardboard disk rotates, the lazy susan will rotate.

This coming week, I plan to start building the final design. I will also modify the code so that the motor doesn't respond for such small changes in the sensor output values. I plan on changing the code so that the motor only rotates when the difference between the sensors is greater than 75. This means that the motor will not rotate all the time but rather when there is a clear difference in lighting

2 comments:

  1. Hey Miranda, what is the number that you are getting out of the photoresistors? The ones that are like 500ish or so?

    ReplyDelete