Advertisement
seston

voltage flipping style sensor

Feb 25th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #define voltageFlipPin1 6
  2. #define voltageFlipPin2 7
  3. #define sensorPin 0
  4.  
  5. int flipTimer = 1000;
  6.  
  7. void setup(){
  8. Serial.begin(9600);
  9. pinMode(voltageFlipPin1, OUTPUT);
  10. pinMode(voltageFlipPin2, OUTPUT);
  11. pinMode(sensorPin, INPUT);
  12.  
  13. }
  14.  
  15.  
  16. void setSensorPolarity(boolean flip){
  17. if(flip){
  18. digitalWrite(voltageFlipPin1, HIGH);
  19. digitalWrite(voltageFlipPin2, LOW);
  20. }else{
  21. digitalWrite(voltageFlipPin1, LOW);
  22. digitalWrite(voltageFlipPin2, HIGH);
  23. }
  24. }
  25.  
  26.  
  27. void loop(){
  28.  
  29. //
  30. setSensorPolarity(true);
  31. delay(flipTimer);
  32. int val1 = analogRead(sensorPin);
  33. delay(flipTimer);
  34. setSensorPolarity(false);
  35. delay(flipTimer);
  36. // invert the reading
  37. int val2 = 1023 - analogRead(sensorPin);
  38. //
  39. reportLevels(val1,val2);
  40.  
  41. }
  42.  
  43.  
  44. void reportLevels(int val1,int val2){
  45.  
  46. int avg = (val1 + val2) / 2;
  47.  
  48. String msg = "avg: ";
  49. msg += avg;
  50. Serial.println(msg);
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement