Advertisement
brownj

lcd1

Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(12, 11, 16, 17, 18, 19);
  4. const int photoPin = A1;
  5. int photoVal = 0;
  6.  
  7. const int controlPin1 = 2; // connected to pin 7 on the H-bridge
  8. const int controlPin2 = 3; // connected to pin 2 on the H-bridge
  9. const int enablePin = 9; // connected to pin 1 on the H-bridge
  10. const int directionSwitchPin = 4; // connected to the switch for direction
  11. const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
  12. const int potPin = A0; // connected to the potentiometer's output
  13.  
  14. // create some variables to hold values from your inputs
  15. int onOffSwitchState = 0; // current state of the On/Off switch
  16. int previousOnOffSwitchState = 0; // previous position of the on/off switch
  17. int directionSwitchState = 0; // current state of the direction switch
  18. int previousDirectionSwitchState = 0; // previous state of the direction switch
  19.  
  20. int motorEnabled = 0; // Turns the motor on/off
  21. int motorSpeed = 0; // speed of the motor
  22. int motorDirection = 1; // current direction of the motor
  23.  
  24. void setup() {
  25. // put your setup code here, to run once:
  26. Serial.print (9600);
  27. lcd.begin (16, 2);
  28. for (int i = 0; i <= 15; i++)
  29. {
  30.  
  31. lcd.setCursor (i, 0);
  32. lcd.print ("James");
  33. delay(300);
  34. lcd.clear();
  35. }
  36. }
  37.  
  38. void loop() {
  39. // put your main code here, to run repeatedly:
  40.  
  41. photoVal = analogRead (photoPin);
  42. lcd.print ("the value is");
  43. lcd.setCursor(6, 1);
  44. lcd.print (photoVal);
  45. delay(50);
  46. lcd.clear();
  47.  
  48. onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  49. delay(1);
  50. Serial.println(onOffSwitchState);
  51. // read the value of the direction switch
  52. directionSwitchState = digitalRead(directionSwitchPin);
  53. Serial.println(directionSwitchState);
  54. // read the value of the pot and divide by 4 to get
  55. // a value that can be used for PWM
  56. motorSpeed = analogRead(potPin);
  57. motorSpeed = map (motorSpeed, 0, 1023, 0, 255);
  58. motorSpeed = constrain (motorSpeed, 0, 255);
  59.  
  60. // if the on/off button changed state since the last loop()
  61. if (onOffSwitchState != previousOnOffSwitchState) {
  62. // change the value of motorEnabled if pressed
  63. if (onOffSwitchState == HIGH) {
  64. motorEnabled = !motorEnabled;
  65. }
  66. }
  67.  
  68. // if the direction button changed state since the last loop()
  69. if (directionSwitchState != previousDirectionSwitchState) {
  70. // change the value of motorDirection if pressed
  71. if (directionSwitchState == HIGH) {
  72. motorDirection = !motorDirection;
  73. }
  74. }
  75.  
  76. // change the direction the motor spins by talking
  77. // to the control pins on the H-Bridge
  78. if (motorDirection == 1) {
  79. digitalWrite(controlPin1, HIGH);
  80. digitalWrite(controlPin2, LOW);
  81. } else {
  82. digitalWrite(controlPin1, LOW);
  83. digitalWrite(controlPin2, HIGH);
  84. }
  85.  
  86. // if the motor is supposed to be on
  87. if (motorEnabled == 1) {
  88. // PWM the enable pin to vary the speed
  89. analogWrite(enablePin, motorSpeed);
  90. } else { // if the motor is not supposed to be on
  91. //turn the motor off
  92. analogWrite(enablePin, 0);
  93. }
  94. // save the current On/Offswitch state as the previous
  95. previousDirectionSwitchState = directionSwitchState;
  96. // save the current switch state as the previous
  97. previousOnOffSwitchState = onOffSwitchState;
  98.  
  99.  
  100. lcd.clear();
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement