Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // TurnRate Sensor: read data from analog gyroscope, output heading on lcd
  2.  
  3. /* Connections:
  4. E : Pin 3
  5. R/W : not connected
  6. R/S : Pin 4
  7. DB4 : Pin 6
  8. DB5 : Pin 7
  9. DB6 : Pin 8
  10. DB7 : Pin 9
  11.  
  12.  
  13. Turn-Rate Sensor: A0
  14. */
  15. // include LCD functions:
  16. #include <LiquidCrystal.h>
  17.  
  18. // define the LCD screen
  19. LiquidCrystal lcd(4, 3, 6, 7, 8, 9);
  20.  
  21. /* global variables: */
  22. // current value on A0
  23. int analogValue;
  24. int analogValue_RelaxedState;
  25. long wholeRotationHeading = 1200000;
  26. long heading;
  27. int headingInDegrees;
  28. long lastMeasurementMillis;
  29.  
  30. // initialization
  31. void setup()
  32. {
  33. // LCD has 4 lines with 20 chars
  34. lcd.begin(20, 4);
  35. lcd.print("system ready");
  36.  
  37. delay(500);
  38. analogValue_RelaxedState = analogRead(A0);
  39. }
  40.  
  41. void loop()
  42. {
  43. // read the current analog value
  44. analogValue = analogRead(A0);
  45. if(abs(analogValue - analogValue_RelaxedState) < 10){
  46. analogValue = analogValue_RelaxedState;
  47. }
  48. int rotationRate = (analogValue - analogValue_RelaxedState);
  49. // calculate heading
  50. long millisSinceLastMeasurement = (millis() - lastMeasurementMillis);
  51. if(millisSinceLastMeasurement > 100){
  52. heading = ((heading + (millisSinceLastMeasurement * rotationRate)) % wholeRotationHeading);
  53. if(heading < 0){
  54. heading = (wholeRotationHeading - heading);
  55. }
  56. headingInDegrees = (heading / (wholeRotationHeading / 360));
  57. lastMeasurementMillis = millis();
  58. }
  59. // implement your code here:
  60. lcd.clear();
  61. lcd.print("Analog 0: ");
  62. lcd.print(analogValue);
  63. lcd.setCursor(0, 1);
  64. lcd.print("turn-rate: ");
  65. lcd.print(rotationRate);
  66. lcd.setCursor(0, 2);
  67. lcd.print("heading: ");
  68. lcd.print(heading);
  69. lcd.setCursor(0, 3);
  70. lcd.print("heading: ");
  71. lcd.print(headingInDegrees);
  72. lcd.print("deg");
  73. delay(40);
  74. }
  75.  
  76.  
  77. /* Usefull LCD functions:
  78. set the current write position of the lcd to specific line and row:
  79. lcd.setCursor(row, line);
  80.  
  81. clear the lcd:
  82. lcd.clear();
  83.  
  84. write onto lcd, starting at current position:
  85. lcd.print("abc");
  86. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement