Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 3
- // outside leads to ground and +5V
- int raw = 0; // variable to store the raw input value
- int Vin = 5; // variable to store the input voltage
- float Vout = 0; // variable to store the output voltage
- float R1 = 10; // variable to store the R1 value
- float R2 = 0; // variable to store the R2 value
- float R3 = 0; // variable to store the R3
- float R4 = 0; // variable to store the R4 value, change R2 to R3
- float buffer = 0; // buffer variable for calculation
- void setup()
- {
- Serial.begin(9600); // Setup serial
- digitalWrite(13, HIGH); // Indicates that the program has intialized
- }
- void loop()
- {
- raw = analogRead(analogPin); // Reads the Input PIN
- Vout = (5.0 / 1023.0) * raw; // Calculates the Voltage on the Input PIN
- buffer = (Vin / Vout) - 1;
- R2 = R1 / buffer;
- delay(250); // time delay 1/4 second
- raw = analogRead(analogPin); // Reads the Input PIN
- Vout = (5.0 / 1023.0) * raw; // Calculates the Voltage on the Input PIN
- buffer = (Vin / Vout) - 1;
- R3 = R1 / buffer;
- R4 = (R3-R2); //R4 equals difference in R2 to R3
- if (R4 < 0) // switches output based on value on R4, if negative, else positive
- {
- digitalWrite(11, LOW);
- digitalWrite(12, HIGH);
- }
- else
- {
- digitalWrite(12,LOW);
- digitalWrite(11, HIGH);
- }
- Serial.print("Voltage: "); //
- Serial.println(Vout); // Outputs the information
- Serial.print("R2: "); //
- Serial.println(R2); //
- Serial.print("R3: "); //
- Serial.println(R3); //
- Serial.print("R4: "); //
- Serial.println(R4); //
- delay(1000);
- }
- /*Credit to lxmyers for resistance input and buffer calculations
- http://arduino.cc/forum/index.php/topic,21614.0.html*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement