Step 4Part 3: Programing and circuit design
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); // LCD on pins 12, 11, 10, 5, 4, 3, 2.
int StartPin = 9; // 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 = 13; // Motor direction select
int DirSwCounter = 0;
int LastDirState = 15;
int Dir = 14;
int cansCrushed; // 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);
lcd.begin(16, 2);
lcd.print("Can Crusher MKII");
delay(3000);
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);
cansCrushed = 0;
}
void loop()
{
// READ PINS
int DirState = digitalRead(DirPin);
if (LastDirState == LOW && DirState == HIGH)
{
DirSwCounter++;
}
LastDirState = DirState;
// PROCESS
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(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
} else {
digitalWrite(enablePin, LOW);
}
}
The wiring diagram for the machine is shown below.
For this design I used Fritzing, it is pretty awesome.
http://www.fritzing.org
Build the circuit board from this design, but don't add the switches yet.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|














































