Advertisement
manvi_m

combination of lcd and dc motor

Jun 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /*
  2. * Manvi Mittal
  3. * Combination of LCD and Motor
  4. */
  5.  
  6. #include <LiquidCrystal.h>
  7.  
  8. LiquidCrystal lcd (10, 19, 11, 12, 13, 7);
  9.  
  10. const int photoPin = A3;
  11. int photoVal = 0;
  12.  
  13. const int controlPin1 = 2;
  14. const int controlPin2 = 3;
  15. const int enablePin = 9;
  16. const int directionSwitchPin = 4;
  17. const int onOffSwitchStateSwitchPin = 5;
  18. const int potPin = A0;
  19.  
  20. int onOffSwitchState = 0;
  21. int previousOnOffSwitchState = 0;
  22. int directionSwitchState = 0;
  23. int previousDirectionSwitchState = 0;
  24. int motorEnabled = 0; //Turns the motor on/off
  25. int motorSpeed = 0;
  26. int motorDirection = 1;
  27.  
  28. void setup() {
  29. // put your setup code here, to run once:
  30. lcd.begin (16, 2);
  31. for (int i = 0; i <= 11; i++)
  32. {
  33. lcd.setCursor (i, 0);
  34. lcd.print ("Manvi");
  35. delay (500);
  36. lcd.clear ();
  37. }
  38. pinMode (directionSwitchPin, INPUT);
  39. pinMode (onOffSwitchStateSwitchPin, INPUT);
  40. pinMode (potPin, INPUT);
  41. pinMode (controlPin1, OUTPUT);
  42. pinMode (controlPin2, OUTPUT);
  43. pinMode (enablePin, OUTPUT);
  44.  
  45. Serial.begin (9600);
  46.  
  47. digitalWrite (enablePin, LOW);
  48. }
  49.  
  50. void loop() {
  51. // put your main code here, to run repeatedly:
  52. photoVal = analogRead (photoPin);
  53. lcd.print ("The value is:");
  54. lcd.setCursor (6, 1);
  55. lcd.print (photoVal);
  56. delay (200);
  57. lcd.clear ();
  58.  
  59. onOffSwitchState = digitalRead (onOffSwitchStateSwitchPin); // read the value of the on/off switch
  60. delay (1);
  61. Serial.print(motorEnabled);
  62. directionSwitchState = digitalRead (directionSwitchPin); // read the value of the direction switch
  63. Serial.println (motorDirection);
  64. motorSpeed = analogRead (potPin) / 4; // read the value of the pot and divide by 4 to get a value
  65.  
  66. if (onOffSwitchState != previousOnOffSwitchState)
  67. {
  68. if (onOffSwitchState == HIGH)
  69. {
  70. motorEnabled = !motorEnabled;
  71. }
  72. }
  73. if (directionSwitchState != previousDirectionSwitchState)
  74. {
  75. if (directionSwitchState == HIGH)
  76. {
  77. motorDirection = !motorDirection;
  78. }
  79. }
  80. if (motorDirection == 1)
  81. {
  82. digitalWrite (controlPin1, HIGH);
  83. digitalWrite (controlPin2, LOW);
  84. }
  85. else
  86. {
  87. digitalWrite (controlPin1, LOW);
  88. digitalWrite (controlPin2, HIGH);
  89. }
  90. if (motorEnabled == 1)
  91. {
  92. analogWrite (enablePin, motorSpeed);
  93. }
  94. else
  95. {
  96. analogWrite (enablePin, 0);
  97. }
  98. previousDirectionSwitchState = directionSwitchState;
  99. previousOnOffSwitchState = onOffSwitchState;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement