Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <TINAH_Pins.h>
  2. #include <TINAH_BufferedLCD.h>
  3. #include <TINAH_HBridge.h>
  4. #include <TINAH_Servo.h>
  5.  
  6. // Alternatively, this includes all of the above files.
  7. // #include <TINAH.h>
  8.  
  9. /*
  10. * Declare object for liquid crystal display.
  11. *
  12. * You must later call begin for it to work.
  13. */
  14. TINAH::BufferedLCD LCD;
  15.  
  16. //Declare objects for DC motors. Use motor output channel as parameter.
  17. TINAH::HBridge leftMotor(2);
  18. TINAH::HBridge rightMotor(3);
  19.  
  20. /**
  21. * Declare objects for servo motors. Use the predefined pins from the
  22. * TINAH::pins::servo array, or choose your own - the Servo library doesn't need
  23. * the pins to have PWM.
  24. */
  25. TINAH::Servo servo0(TINAH::pins::servo[0]);
  26. TINAH::Servo servo1(15);
  27.  
  28. void setup() {
  29. /*
  30. * Initializes the BufferedLCD class and sends setup commands to the display.
  31. *
  32. * Note that the size of the display is no longer provided, it is fixed to 16x2.
  33. *
  34. * The first argument is true to use the deferred update mode. Deferred update
  35. * mode requires calling the updateIncremental method while not using it means
  36. * text updates immediately, but printing will block other code as data is sent.
  37. *
  38. * The second argument is false to use time delays rather than reading the busy
  39. * status from the LCD. You can change this to be true to make LCD writes faster,
  40. * but if the LCD is not present or malfunctions, the code will get stuck waiting
  41. * for it to become ready in non-deferred mode.
  42. */
  43. LCD.begin(true, false);
  44.  
  45. LCD.setCursor(0, 0);
  46. //Wrap strings for printing in F() to save RAM.
  47. //Newlines should work properly now
  48. LCD.print(F("TINAH Demo\nStart for motor"));
  49.  
  50. /*
  51. * When using the deferred update mode, flush() makes sure the contents are fully
  52. * displayed after the call returns.
  53. */
  54. LCD.flush();
  55. delay(500);
  56. LCD.clear();
  57. }
  58.  
  59. bool motorEnabled = false;
  60.  
  61. void loop() {
  62. //If you don't know the correct type to use for a variable, auto can guess.
  63. auto loopStart = millis();
  64.  
  65. int16_t knobA = analogRead(6);
  66. int16_t knobB = analogRead(7);
  67. uint8_t servoOutput = ((int32_t)knobA * 180) / 1024;
  68. int16_t motorOutput = (knobB - 512) / 2;
  69.  
  70. if (!digitalRead(TINAH::pins::startButton)) {
  71. motorEnabled = true;
  72. }
  73. if (!digitalRead(TINAH::pins::stopButton)) {
  74. motorEnabled = false;
  75. }
  76.  
  77. servo0.write(servoOutput);
  78. if (motorEnabled) {
  79. leftMotor.setOutput(motorOutput);
  80. rightMotor.setOutput(-motorOutput);
  81. }
  82. else {
  83. TINAH::HBridge::allOff();
  84. }
  85.  
  86. LCD.clear();
  87.  
  88. LCD.setCursor(0, 0);
  89. LCD.print(F("A:"));
  90. LCD.print(knobA);
  91. LCD.setCursor(0, 1);
  92. LCD.print(F("B:"));
  93. LCD.print(knobB);
  94.  
  95. LCD.setCursor(6, 0);
  96. LCD.print(F("Servo:"));
  97. LCD.print(servoOutput);
  98.  
  99. LCD.setCursor(6, 1);
  100. LCD.print(F("Motor:"));
  101. if (motorEnabled) {
  102. LCD.print(motorOutput);
  103. }
  104. else {
  105. LCD.print(F("off"));
  106. }
  107.  
  108. //Loop speed regulation, and at the same time updating the LCD
  109. while (millis() < loopStart + 10) {
  110. //If there is nothing left to do this method returns false.
  111. LCD.updateIncremental();
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement