I tried to write a code to operate an automatic can crusher with an LCD readout so it can tell how many cans it has crushed. After designing the circuit and writing the code from examples I cannot get it to work--count the cans it crushes (if the number of switch count is divisabe by 2 one can has been crushed). Instead of counting the switch and seeing if it is a factor of 2, it just adds the number of cans without end.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); // LCD on pins 12, 11, 10, 5, 4, 3, 2.
int StartPin = 13; // switch input
int motor1Pin = 7; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 6; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 8; // H-bridge enable pin
int DirPin = 1; // Motor direction select
//int DirState = 0;
int DirSwCounter = 0;
int LastDirState = LOW;
int Dir = LOW;
int cansCrushed = 0; // Initial number of cans crushed set to 0
void setup()
{
lcd.print("Can Crusher MKII");
delay(3000);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Crushed:");
lcd.setCursor(10, 0);
lcd.print((int)cansCrushed);
lcd.setCursor(0, 1);
lcd.print("Weight:");
lcd.setCursor(9, 1);
lcd.print((int)cansCrushed*.034375);
pinMode(StartPin, INPUT);
pinMode(DirPin, INPUT);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
}
void loop()
{
int DirState = digitalRead(DirPin);
if (DirState =! LastDirState){
if (DirState == HIGH){
DirSwCounter++;
}
}
LastDirState = DirState;
if (DirSwCounter % 2 == 0){
digitalWrite(Dir, LOW);
cansCrushed++;
} else {
digitalWrite(Dir, HIGH);
}
if (digitalRead(StartPin == HIGH) && digitalRead(Dir == LOW)){
digitalWrite(enablePin, HIGH);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
else if (digitalRead(StartPin == HIGH) && digitalRead(Dir == HIGH)){
digitalWrite(enablePin, HIGH);
digitalWrite(motor2Pin, HIGH);
}
else {
digitalWrite(enablePin, LOW);
}
lcd.setCursor(10, 0);
lcd.print((int)cansCrushed);
lcd.setCursor(9, 1);
lcd.print((int)cansCrushed*.034375);
}
Thanks,
matrhint