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
Tuesday, February 14, 2017
Tuesday, February 7, 2017
Voltage Divider with Four Photoresistors and Servo Motor Connections
This past week I worked on writing code that would read in
values from four different photoresistors and print them on the serial monitor
of the Arduino. To do this, I made the circuit shown in figures 1 and 2. This
circuit includes four photoresistors set up in a voltage divider and the output
of each voltage divider is connected to Analog pins 0-3 on the Arduino.
Figure 1: Schematic of voltage divider circuit with four photoresistors and four 10K ohm resistors. |
Figure 2: Voltage divider circuit with four photoresistors and four 10K ohm resistors. |
The C code that reads the output values from each of the
four photoresistors and prints the one that is exposed to the most light
(Greatest output value) is included below:
void setup()
{
Serial.begin(9600); //Begin serial communication
}
void loop()
{
// reads the
input on analog pins 0-3
int sensorValue0 = analogRead(A0);
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);
// prints
the headings of the columns
Serial.println();
Serial.print("Sensor 1");
Serial.print("\t");
Serial.print("Sensor 2");
Serial.print("\t");
Serial.print("Sensor 3");
Serial.print("\t");
Serial.print("Sensor 4");
Serial.print("\t");
Serial.println("The Sensor with most
light");
//prints the
values from each of the four voltage dividers
Serial.print(sensorValue0);
Serial.print("\t");
Serial.print("\t");
Serial.print(sensorValue1);
Serial.print("\t");
Serial.print("\t");
Serial.print(sensorValue2);
Serial.print("\t");
Serial.print("\t");
Serial.print(sensorValue3);
Serial.print("\t");
Serial.print("\t");
/*Finds the
photoresistor that is exposed to the most amount
of light which is the highest output value
and prints the
the number of the photoresistor
*/
int greatestValue;
if (sensorValue0 > sensorValue1)
if (sensorValue0 > sensorValue2)
if (sensorValue0 > sensorValue3)
//Serial.print("Sensor 1");
greatestValue = 1;
if (sensorValue1 > sensorValue0)
if (sensorValue1 > sensorValue2)
if (sensorValue1 > sensorValue3)
//Serial.print("Sensor 2");
greatestValue = 2;
if (sensorValue2 > sensorValue0)
if (sensorValue2 > sensorValue1)
if (sensorValue2 > sensorValue3)
//Serial.print("Sensor 3");
greatestValue = 3;
if (sensorValue3 > sensorValue0)
if (sensorValue3 > sensorValue1)
if (sensorValue3 > sensorValue2)
//Serial.print("Sensor 4");
greatestValue = 4;
Serial.print(greatestValue);
delay(1000);
}
___________________________________________________________________________________
Example of the output on the Serial Monitor is shown in the
Figure below.
I am in the process of figuring out exactly what these numbers mean. The relationship between these output values and the amount of light is that the more the sensor is exposed to light, the greater the output value. This is opposite from the resistance because the more the sensor is exposed to light, the lower the resistance.
The servo motor came in this week and I started to play with
the motor to see how it works. The following link was very helpful with
understanding how to connect the servo motor to the Arduino and the figures
below show the proper connections(Figures 4 and 5): https://www.arduino.cc/en/Tutorial/Sweep
The link includes example code that is also found in the
Arduino Examples that rotates the motor 180 degrees in one direction and then
180 degrees in the other direction.
Figure 4 shows the proper connections between the Arduino and the servo motor. |
Figure5 shows the schematic of the proper connections between the Arduino and the servo motor |
When running this example code, I discovered that the servo
motor only turns 180 degrees (90 degrees in each direction). This is a problem
because if the tiny servo motor turns 180 degrees, this would only turn the
Lazy Susan a few degrees. I will have to come up with a design that works like
a gear system so that I am able to turn the Lazy Susan 180 degrees.
This coming week, I will figure out a design to turn the
Lazy Susan more than just a few degrees. I will also modify the code so that
when photoresistor 1 or 2 is receiving the most light, then the motor turns one
direction and when photoresistor 3 or 4 is receiving the most light, the motor
turns in the opposite direction.
Subscribe to:
Posts (Atom)