Hiya. (please delete if not allowed)
so I have an assignment to virtually
a) control a conveyor belt (simulated by a 9v motor)
b) count the items passing (use me hand/phone ect)
c) have a push button for "mode 1" or "mode 2"
d) mode1 stops motor after 3 items have passed
e) mode 2 stops motor after 6 items have passed
(so I thought it would be nice to use the lcd to display the number of items that have passed, -unnecessary for brief)
I have an Ultrasound sensor, HC-SR04, a 16/2 lcd, a push button and a 9V motor circuit, using a mosfet, all wired up (very tight fit!)
My problem(s) are many and varied, but for the purpose of this conversation.... I have a patchwork of code (bits taken from different prog's ive made in past and stuff i found online) and I am struggling to get it to all work together.
I have the system actually counting items, (which i thought would be the hardest part) but i cant seem to get the push button to do anything (even print its state to the monitor)
could somebody please give a look at my code (ill put up a pic of wiring too but I dunno if that will be of any help) and suggest where Im wrong and maybe what i could do to finish this little project?
Please and thank you (for reading this far and any help you can give!)
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
#define trigPin 9
#define echoPin 10
#define motorPin 3
#define buttonPin 13
int counter = 0;
int currentState = 0;
int previousState = 0;
int motorSpeed = 0;
void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);// initialize the motor pin as an output:
Serial.begin (9600);
lcd.print("items=");
}
void loop() {
int buttonState = 0;
// initialize the LED pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
Serial.print(buttonState);
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 30){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.print(counter);
}
}
for (motorSpeed = 0 ; motorSpeed <= 255; motorSpeed += 10)
{
analogWrite(motorPin, motorSpeed);
delay(30);
}
for (motorSpeed = 255 ; motorSpeed >= 0; motorSpeed -= 10)
{
analogWrite(motorPin, motorSpeed);
delay(30);
}
}