Advertisement
vawrinekm20

DC Motor and LCD Code

Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /*Matt Vawrinek
  2. * 6/22/17
  3. * DC motor and LCD combined code
  4. */
  5. const int controlPin1 = 2;
  6. const int controlPin2 = 3;
  7. const int enablePin = 9;
  8. const int directionSwitchPin = 4;
  9. const int onOffSwitchStateSwitchPin = 5;
  10. const int potPin = A0;
  11. int onOffSwitchState = 0;
  12. int previousOnOffSwitchState = 0;
  13. int directionSwitchState = 0;
  14. int previousDirectionSwitchState = 0;
  15. int motorEnabled = 0;
  16. int motorSpeed = 0;
  17. int motorDirection =1;
  18.  
  19. #include <LiquidCrystal.h>
  20.  
  21. LiquidCrystal lcd(12, 11, 15, 16, 17, 18); // 15-18 are A1-A4, setting the pin numbers up, don't name GND and positive, start with side closest to the edge, from the top down from the LCD
  22. const int photoPin = A5;
  23. int photoVal = 0;
  24.  
  25. void setup() {
  26. // put your setup code here, to run once:
  27. pinMode(potPin, INPUT);
  28. pinMode(directionSwitchPin, INPUT);
  29. pinMode(onOffSwitchStateSwitchPin, INPUT);
  30. pinMode(controlPin1, OUTPUT);
  31. pinMode(controlPin2, OUTPUT);
  32. pinMode(enablePin, OUTPUT);
  33. //pull the enable pin LOW to start
  34. digitalWrite(enablePin, LOW);
  35. Serial.begin(9600);
  36. // put your setup code here, to run once:
  37. pinMode(photoPin, INPUT);
  38. Serial.begin(9600);
  39. lcd.begin(16, 2);// dimensions
  40. ;
  41. for(int i = 0; i <= 13; i++) //first 12 numbers can fit on first row, ++ means go up by one
  42. {
  43. lcd.setCursor(i, 0);
  44. lcd.print("Matt");
  45. delay(300);
  46. lcd.clear();
  47. }
  48. lcd.clear();
  49.  
  50.  
  51.  
  52. }
  53.  
  54. void loop() {
  55. // put your main code here, to run repeatedly:
  56.  
  57. onOffSwitchState =digitalRead(onOffSwitchStateSwitchPin);// read the value of the on/off switch
  58. delay(1);
  59. // Serial.println(onOffSwitchState);
  60. directionSwitchState = digitalRead(directionSwitchPin); //read the value of the direction
  61. Serial.println(directionSwitchState);
  62. motorSpeed = analogRead(potPin); //setting how fast the fan is spinning
  63. motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
  64. motorSpeed = constrain(motorSpeed, 0, 255);
  65. if (onOffSwitchState != previousOnOffSwitchState){
  66. if (onOffSwitchState == HIGH){
  67. motorEnabled = !motorEnabled;
  68. }
  69. }
  70.  
  71. if (directionSwitchState != previousDirectionSwitchState) {
  72. if (directionSwitchState == HIGH) {
  73. motorDirection = !motorDirection;
  74. }
  75. }
  76.  
  77. if(motorDirection ==1) {
  78. digitalWrite(controlPin1, HIGH);
  79. digitalWrite(controlPin2, LOW);
  80. }
  81.  
  82. else {
  83. digitalWrite(controlPin1, LOW);
  84. digitalWrite(controlPin2, HIGH);
  85. }
  86. if (motorEnabled ==1) {
  87. analogWrite(enablePin, motorSpeed);
  88. }
  89. else {
  90. analogWrite(enablePin, 0);
  91. }
  92. Serial.println(motorSpeed);
  93. previousDirectionSwitchState =
  94. directionSwitchState,
  95. previousOnOffSwitchState = onOffSwitchState;
  96.  
  97. // put your main code here, to run repeatedly:
  98. photoVal = analogRead(photoPin); //read photoresistor value, since photoresistor is input and it its values are in a range, which is analog//
  99. lcd.print("The value is: ");
  100. lcd.setCursor(6, 1);
  101. lcd.print(photoVal);
  102. delay(100);
  103. lcd.clear();
  104. //for(int i = 0; i <= 13; i++) //first 12 numbers can fit on first row, ++ means go up by one
  105. {
  106. // lcd.setCursor(i, 0);
  107. // lcd.print("Matt");
  108. // delay(300);
  109. // lcd.clear();
  110. }
  111. // lcd.clear();
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement