for this project I can keep it simple,
if button is pressed (motor stops after count reaches 6)and if button not pressed (stops after 3, )
system will start when... you plug it in bascily? it runs from the start
(sorry if im not being clear)
for this project I can keep it simple,
if button is pressed (motor stops after count reaches 6)and if button not pressed (stops after 3, )
system will start when... you plug it in bascily? it runs from the start
(sorry if im not being clear)
so when it starts you need to hold the button down for the 6 case
really?
yup
its just proof of concept i guess
now im thinking the button isnt wired up correctly? its not doing anything, I dont know why i wired it to a0?
(i think it was on a2 or a3 before i changed it to the better diagonal way, )
there is no a0, a2 or a3 mentioned anywhere in the code???
this is my code so far .....
#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;
int lastButtonState = HIGH; // the previous state from the input pin
int currentButtonState; // the current reading from the input pin
void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);// initialize the motor pin as an output:
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
Serial.begin (9600);
lcd.print("items=");
}
void loop() {
currentButtonState = digitalRead(buttonPin);// read the state of the switch/button:
lastButtonState = currentButtonState; // save the last state??????
if (digitalRead(buttonPin) == 0 && counter <= 3){
analogWrite(motorPin, 255);//turning on motor untill count =3 + button not pressed
}
if (digitalRead(buttonPin) == 1 && counter <= 6){
analogWrite(motorPin, 255);//turning on motor untill count =6 + button pressed
}
else{
analogWrite(motorPin, 0);//motor is off
}
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);
}
}
}
I really appreciate everyone's help so far, sorry ye have to deal with a programming simpleton, I feel like it is getting there though!
pin 13 is the Arduino builtin LED
ok so pin 8, 11 & 12 are free? and the other wire from the button? leave it in a0?
you can use an analog pin just line a digital pin. configure is as INPUT_PULLUP, use digitalRead()
I see one wire coming from the button to A0. I don't see the diagonal leg connected to GND.
Is it my eyesight, or is that ground connection missing?
Why do you have these two lines of code? You never use either variable:
currentButtonState = digitalRead(buttonPin);// read the state of the switch/button:
lastButtonState = currentButtonState; // save the last state??????
There is no reason to turn the motor on over and over again in loop(). Why don't you just turn it on once in setup() and just turn it off in loop()?
if (digitalRead(buttonPin) == 0 && counter <= 3) {
analogWrite(motorPin, 255);//turning on motor untill count =3 + button not pressed
}
if (digitalRead(buttonPin) == 1 && counter <= 6) {
analogWrite(motorPin, 255);//turning on motor untill count =6 + button pressed
}
else {
analogWrite(motorPin, 0);//motor is off
}
I'm still not sure what the delay is for and you are never copying currentState into previousState which means if you hold you hand in front of the sensor count will continue to increment even though it is only one object.
delay(100);
if (currentState != previousState) {
if (currentState == 1) {
counter = counter + 1;
lcd.print(counter);
}
}
yeah ground is missing, i must have messed up when trying to change something (assuming i got it right at the start!)
one leg is going to a0, and the other leg to pin 13. so the one going to a0 should be ground, and the one on 13 should be.... pin 8?
so i thought the delay was so it didnt count too fast, as you say it does count continuously if my hand stays there,
ill try to adjust the code to your suggestions, they make perfect sence, how would i go about copying currentState into previousState?
if (currentState != previousState) {
if (currentState == 1) {
counter = counter + 1;
lcd.print(counter);
}
previousState = currentState;
}
Now it won't count again until the the distance is greater than 30 and then less than 30 again.
One leg goes to what ever pin you define as buttonPin and read with digitalRead() to determine the state.
#define buttonPin 13 //A0? 8?
and the other leg diagonally across the button goes to ground.
I know I shouldn't write code for you but I think you need to see it properly coded. It compiles but I don't have the hardware to test. Pay attention that the logic I have used is exactly the steps if you think it out logically in your head. I feel like you were just adding code and taking shots in the dark. Always try to use a naturally logical sequence. Also note that I have more clearly named and properly scoped the variables used in this sketch. If you don't have the button pushed during reset the maximum number of items is 3. If you hold the button while you reset the maximum number of items is 6. You do not have to continue to hold the button while the motor is running. Once the maximum number of items is detected the motor is turned off and an infinite loop is entered until the board is reset.
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
#define trigPin 9
#define echoPin 10
#define motorPin 3
#define buttonPin 13
int maxItems = 3;
void setup()
{
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);// initialize the motor pin as an output:
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
Serial.begin (9600);
lcd.print("items=");
// Detect 6 items if button is pressed on startup
if (digitalRead(buttonPin) == LOW)
{
maxItems = 6;
}
// Start the motor
analogWrite(motorPin, 255);
}
void loop()
{
static int numItems = 0;
static bool previousItemDetected = false;
bool itemDetected;
long duration, distance;
// Read sensor and calculate distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Determine if item is detected
if (distance <= 30)
{
itemDetected = true;
}
else
{
itemDetected = false;
}
// Check for detection change
if (itemDetected != previousItemDetected)
{
// Item either showed up or went away so save the last detection flag
previousItemDetected = itemDetected;
if (itemDetected)
{
// New item detected
numItems++;
lcd.print(numItems);
}
}
// Check to see if the maximum number of items is detected
if (numItems >= maxItems)
{
// Turn motor off
analogWrite(motorPin, 0);
// Do nothing until board is reset
while (1);
}
}
First off thank you very much! that is exactly what i needed.
secondly, i didnt realise yere not allowed to write code for someone, sorry that is basicly what i was here looking for. My bad.
this is exactly how i was doing it! 100%
i have tested it on my hardware (which is working now thanks to here) and it does work exactly as you said!
thanks again man
perfect thanks!
We are really here to help, not implement. There are a lot of folks that come here wanting to get their homework done for them. I was trying to illustrate that you were making it more complicated than it is and the logic is more in line with thinking it through in your head.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.