Advertisement
Clarvel

light following car

Oct 23rd, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 3
  2. // outside leads to ground and +5V
  3. int raw = 0; // variable to store the raw input value
  4. int Vin = 5; // variable to store the input voltage
  5. float Vout = 0; // variable to store the output voltage
  6. float R1 = 10; // variable to store the R1 value
  7. float R2 = 0; // variable to store the R2 value
  8. float R3 = 0; // variable to store the R3
  9. float R4 = 0; // variable to store the R4 value, change R2 to R3
  10. float buffer = 0; // buffer variable for calculation
  11.  
  12. void setup()
  13. {
  14. Serial.begin(9600); // Setup serial
  15. digitalWrite(13, HIGH); // Indicates that the program has intialized
  16. }
  17.  
  18. void loop()
  19. {
  20. raw = analogRead(analogPin); // Reads the Input PIN
  21. Vout = (5.0 / 1023.0) * raw; // Calculates the Voltage on the Input PIN
  22. buffer = (Vin / Vout) - 1;
  23. R2 = R1 / buffer;
  24. delay(250); // time delay 1/4 second
  25. raw = analogRead(analogPin); // Reads the Input PIN
  26. Vout = (5.0 / 1023.0) * raw; // Calculates the Voltage on the Input PIN
  27. buffer = (Vin / Vout) - 1;
  28. R3 = R1 / buffer;
  29. R4 = (R3-R2); //R4 equals difference in R2 to R3
  30. if (R4 < 0) // switches output based on value on R4, if negative, else positive
  31. {
  32. digitalWrite(11, LOW);
  33. digitalWrite(12, HIGH);
  34. }
  35. else
  36. {
  37. digitalWrite(12,LOW);
  38. digitalWrite(11, HIGH);
  39. }
  40. Serial.print("Voltage: "); //
  41. Serial.println(Vout); // Outputs the information
  42. Serial.print("R2: "); //
  43. Serial.println(R2); //
  44. Serial.print("R3: "); //
  45. Serial.println(R3); //
  46. Serial.print("R4: "); //
  47. Serial.println(R4); //
  48. delay(1000);
  49. }
  50. /*Credit to lxmyers for resistance input and buffer calculations
  51. http://arduino.cc/forum/index.php/topic,21614.0.html*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement