Advertisement
Maderdash

canCrusher

Dec 7th, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. // Latest v1.7 as of 12.3.11pm
  3. LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); // LCD pin rs rw en d4 d5 d6 d7.
  4. int StartPin                = 9;      // switch input
  5. int motor1Pin               = 7;      // H-bridge leg 1 (pin 2, 1A)
  6. int motor2Pin               = 6;      // H-bridge leg 2 (pin 7, 2A)
  7. int enablePin               = 8;      // H-bridge enable pin
  8. int DirPin                  = 13;     // Motor direction select
  9. int DirSwCounter            = 0;
  10. int LastDirState            = 0;
  11. bool Dir                    = true;
  12. byte runningCan             = 0;
  13. int cansCrushed;                       // Initial number of cans crushed set to 0
  14. byte endStop                = 0;
  15. void writeLCD(int cans) {
  16.   if (cans == 0) {
  17.     lcd.begin(16, 2);
  18.     lcd.print("Adams CanCrshr1");
  19.     delay(3000);
  20.     lcd.clear();
  21.     lcd.setCursor(0, 0);
  22.     lcd.print("Crushed:");
  23.     lcd.setCursor(10, 0);
  24.     lcd.print((int) cansCrushed);
  25.     lcd.setCursor(0, 1);
  26.     lcd.print("Weight:");
  27.     lcd.setCursor(9, 1);
  28.     lcd.print((int) cansCrushed * .034375);
  29.   } else {
  30.     lcd.begin(16, 2);
  31.     lcd.print("Adams CanCrshr1");
  32.     delay(3000);
  33.     lcd.clear();
  34.     lcd.setCursor(0, 0);
  35.     lcd.print("Crushed:");
  36.     lcd.setCursor(10, 0);
  37.     lcd.print((int) cans);
  38.     lcd.setCursor(0, 1);
  39.     lcd.print("Weight:");
  40.     lcd.setCursor(9, 1);
  41.     lcd.print((int) cans * .034375);
  42.   }
  43. }
  44.  
  45. void setup()
  46. {
  47.   Serial.begin(9600);
  48.   // INITIALIZE
  49.   pinMode(StartPin, INPUT);
  50.   pinMode(DirPin, INPUT);
  51.   pinMode(motor1Pin, OUTPUT);
  52.   pinMode(motor2Pin, OUTPUT);
  53.   pinMode(enablePin, OUTPUT);
  54.   digitalWrite(enablePin, LOW);
  55.   lcd.begin(16, 2);
  56.   //cansCrushed = 0;
  57. }
  58. /////////////This is where it is done at///////////////////////////
  59. byte canCycler() {
  60.   // Start if statment.
  61.   if (digitalRead(StartPin) == HIGH && runningCan == false) {
  62.     // Check if crusher is ready for operation.
  63.     if (digitalRead(DirPin) == HIGH) {
  64.       digitalWrite(motor1Pin, HIGH);
  65.       digitalWrite(motor2Pin, LOW); // forward motors.
  66.       runningCan = 1; // Passes function to stage 2.
  67.       Serial.println("Crushing started.");
  68.       delay(500);
  69.     }
  70.   }
  71.   //phase 2 return action called.
  72.   if (digitalRead(endStop) == HIGH && runningCan == 1) {
  73.     digitalWrite(motor1Pin, LOW);
  74.     digitalWrite(motor2Pin, HIGH);
  75.     Serial.println("StartPin Enabled and Dir True");
  76.     delay(500);
  77.     runningCan = 2; // Pass the function to stage 3.
  78.   }
  79.   if (DirPin == HIGH && runningCan == 2) {
  80.     digitalWrite(motor1Pin, LOW);
  81.     digitalWrite(motor2Pin, LOW);
  82.     Serial.println("Motor stopped");
  83.   }
  84.   return cansCrushed++; // Add to the total amount of cans crushed.
  85. }
  86.  
  87.  
  88.  
  89. void loop() {
  90.   writeLCD(canCycler());
  91.  
  92.  
  93.  
  94.   //  // READ PINS
  95.   //  int DirState = digitalRead(DirPin);
  96.   //  if (LastDirState == LOW && DirState == HIGH) {
  97.   //    DirSwCounter++;
  98.   //    Serial.println("dirPin triggered");
  99.   //  }
  100.   //  LastDirState = DirState; /// ?????
  101.   //  // PROCESS
  102.   //  if (DirSwCounter % 2 == 0) {
  103.   //    Dir = false;
  104.   //    cansCrushed++;
  105.   //  } else {
  106.   //    Dir = true;
  107.   //  }
  108.   //  if (digitalRead(StartPin) == HIGH && Dir == false) {
  109.   //    digitalWrite(motor1Pin, HIGH);
  110.   //    digitalWrite(motor2Pin, LOW);
  111.   //    Serial.println("Start Enabled and Dir False");
  112.   //  }
  113.   //  else if (digitalRead(StartPin) == HIGH && Dir == true)
  114.   //  {
  115.   //    digitalWrite(motor1Pin, LOW);
  116.   //    digitalWrite(motor2Pin, HIGH);
  117.   //    Serial.println("StartPin Enabled and Dir True");
  118.   //  } else {
  119.   //    //delay(10000);
  120.   //    digitalWrite(motor1Pin, LOW);
  121.   //    digitalWrite(motor2Pin, LOW);
  122.   //    Serial.println("Motor stopped");
  123.   //  }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement