Problems with if/else counting without pause.

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

if (digitalRead(StartPin == HIGH) && digitalRead(Dir == LOW)){

You're reading the wrong pins - sort out your parentheses.

please modify your post, select the code and press the # button

void setup()
{
  lcd.print("Can Crusher MKII");
  delay(3000);
  lcd.begin(16, 2);

You print before you initialized the LCD, might work but counterintuitive

put this part in a separate function

void displayStatus()
{
  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);
}

A quick look at your code with some changes

#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;              <<<<< THIS IS NO PIN NUMBER
int cansCrushed = 0;                       // Initial number of cans crushed set to 0


void setup()
{
  // INITIALIZE
  pinMode(StartPin, INPUT);
  pinMode(DirPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);

  // GET STARTED
  lcd.begin(16, 2);
  lcd.print("Can Crusher MKII");
  delay(3000);
  displayStatus();
}

void loop()
{
  // READ PINS
  int DirState = digitalRead(DirPin);  
  if (LastDirState == LOW && DirState == HIGH)  // you want to detect a rising edge
  {
    DirSwCounter++;
  }
  LastDirState = DirState;

  // PROCESS 
  if (DirSwCounter % 2 == 0)
  {
    digitalWrite(Dir, LOW);     // SEE ABOVE -  int Dir = LOW;  CONFLICT !!! Dir should be initialized to a pinnumber e.g.!!!
    cansCrushed++;
  } else {
    digitalWrite(Dir, HIGH);
  }

  // if (digitalRead(StartPin == HIGH) && digitalRead(Dir == LOW))  // FAULT. you mean probably 
  if (digitalRead(StartPin) == HIGH && digitalRead(Dir == LOW)  
  {
    digitalWrite(enablePin, HIGH);
    digitalWrite(motor1Pin, HIGH);
    digitalWrite(motor2Pin, LOW);
  } 
  // else if (digitalRead(StartPin == HIGH) && digitalRead(Dir == HIGH))  // FAULT. you mean probably 
  else if (digitalRead(StartPin) == HIGH && digitalRead(Dir) == HIGH) 
  {
    digitalWrite(enablePin, HIGH);
    digitalWrite(motor2Pin, HIGH);
  } else {
    digitalWrite(enablePin, LOW);
  }

  displayStatus();
 
}

For pins that don’t need to change you better use

#define DIRPIN 1 iso int dirPin=1; as the latter can be changed. you can just do int x = digitalRead(DIRPIN);

hopes this helps

Thanks guys! I can't wait to try it out!

It gives me a error with the displayStatus(); in the setup and loop, and I see how I messed up with the parentheses. I took out the displayStatus();. Thanks alot agin for your help.