Looking for Project Help

I’m trying to make a project that essentially runs a motor until a certain amount of objects pass through an ir sensor. I cant completely figure it out. I would appreciate some help. Here is my code:

int relay = 9;
int count = 0;
int x = 0;
const int ProxSensor=A0;
int inputVal = 0;


void setup() {
// put your setup code here, to run once:
pinMode(relay, OUTPUT); //declare relay as output
pinMode(13, OUTPUT); // Pin 13 has an LED connected on most Arduino boards:
pinMode(ProxSensor,INPUT); //Pin 2 is connected to the output of proximity sensor
Serial.begin(9600);
}

void loop()
{
Serial.println(analogRead(A0)); // this will print the IR sensor value.
delay(100); // printing delay.

if (x < 5) {
// put your main code here, to run repeatedly:
digitalWrite(relay,LOW); //turn on the motor

if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on

}
else
{
digitalWrite(13, LOW); // set the LED off
x = count++; //counter for how many times led turns on
delay(100); // wait
}
Serial.print(x);
}

else if (x == 5) { //runs loop 5 times then stops
digitalWrite(relay,HIGH); //turn the motor off
exit(0);
}
}

What's the problem?

Please remember to use code tags when posting y

Please use the code button </> when posting code so it looks as follows. Also, if you use the AutoFormat tool to indent the code consistently it makes it much easier to see which parts go together

int relay = 9;
int count = 0;
int x = 0;
const int ProxSensor=A0;
int inputVal = 0;

    \
void setup() {
    // put your setup code here, to run once:
    pinMode(relay, OUTPUT);   //declare relay as output
    pinMode(13, OUTPUT);          // Pin 13 has an LED connected on most Arduino boards:  
    pinMode(ProxSensor,INPUT);    //Pin 2 is connected to the output of proximity sensor
    Serial.begin(9600);
}

void loop() 
{
    Serial.println(analogRead(A0)); // this will print the IR sensor value.
    delay(100); // printing delay.

    if (x < 5) { 
            // put your main code here, to run repeatedly:
        digitalWrite(relay,LOW);  //turn on the motor
        
        if(digitalRead(ProxSensor)==HIGH)      //Check the sensor output
        {
            digitalWrite(13, HIGH);   // set the LED on
            
        }
        else
        {
            digitalWrite(13, LOW);    // set the LED off
            x = count++;  //counter for how many times led turns on
            delay(100);              // wait 
        }
        Serial.print(x);
    }

    else if (x == 5) { //runs loop 5 times then stops
        digitalWrite(relay,HIGH);  //turn the motor off
        exit(0);
    }
}

Why are you using analogRead() with A0 in one line and digitalRead() with the same pin (called proxSensor) on other lines?

…R

I just attached my current code. So right now, I am trying to use a relay and an IR sensor in my setup. Right now I have the relay motor turn on until the IR sensor has counted 5 objects pass past it. Once 5 objects pass, I want the relay to turn off, turning the motor off. I am a bit confused and any programming issues are most likely from me piecing different codes into mine that I thought would work. Right now, it is just counting 5 seconds and not the number of loops. I am lost on how to fix this. Thank you guys

pool_chem2.ino (1.12 KB)

Please post your code.
In code tags.

int relay = 9;
int count = 0;
int x = 0;
const int ProxSensor = 8;
int inputVal = 0;


void setup() {
// put your setup code here, to run once:
pinMode(relay, OUTPUT); //declare relay as output
pinMode(13, OUTPUT); // Pin 13 has an LED connected on most Arduino boards:
pinMode(ProxSensor, INPUT); //Pin 8 is connected to the output of proximity sensor
Serial.begin(9600);
}

void loop()
{
x = 0;
while (x < 5) {
// put your main code here, to run repeatedly:
digitalWrite(relay, LOW); //turn on the motor
//delay(2600);
//digitalWrite(relay,HIGH); //turn the motor off
//delay(2000);

if (digitalRead(ProxSensor) == HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on

}
else
{
digitalWrite(13, LOW); // set the LED off
delay(100);
x = count++; //counter for how many times led turns on
delay(100);
Serial.print(x);
}
// wait

//x++;
}
//for (x == 5;) { //runs loop 5 times then stops
digitalWrite(relay, HIGH); //turn the motor off
exit(0);
}

exit(0); What's that doing there?

Please remember to use code tags when posting code

I used the exit to stop the code so it wouldn't keep looping

I just figured it out. I increased the delay after the count and it now doesn't count as fast and works

jgaiser2:
I just figured it out. I increased the delay after the count and it now doesn't count as fast and works

You have proven to be hard learning and not taking any advice over the last few posts, but still a little note about delay.

You're not allowed to use delay() in your code until you know why that is.

Whatever approach you use to turning the motor off after 5 pieces have passed, do you ever need to be able to restart the process to count another 5 later in the day? Or is this literally only a once-off count to 5 after the Arduino powers up?

(You didn't say what the application is, but I'm picturing a conveyor belt delivering 5 parts to shoot them down a chute into a box, or something along those lines. IRL it's likely you would want to do that many times a day, and re-starting the Arduino is an ugly way of catering for more than one box to be filled.... I'd be very inclined to consider adding a startButton to fire it up again.)

OP I have written a sketch to do what you want that:

  • Uses a state machine approach, which you can read about here
  • Implements the state machine with switch...case
  • Uses the state change detect technique to act when the sensor becomes unblocked
  • Uses blink without delay to blink the built in led to prove the main code doesn't block anything
  • Has a start button, to kick the process off whenever you need it, including the first time

Post back if you want the code...

State diagram below:

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

It looks like the OP has found out a way to make his project work, and has scarpered. He may not have the taken the best route, and he may not have taken advantage of the free advice you guys give out, but don't think it was all in vain. I am sure I am not alone in reading the threads of others to try to make sense of this (these!) crazy computer language(s). I have gained greatly from listening in as you help others, and many others will have gained vicariously too.

Particular thanks to meltDown in thread #11 for posting a link to GrumpyMike's tutorial on state machines, arrays, and the like. Like most people I will stick with the simple code that I know how to use until the simple code is not good enough to do the job at hand, only then will I progress to more 'complex' code.

Anyhow, this is just a note to say thanks for keeping me up late into the night....again!

GM

Glorymill:
It looks like the OP ..... has scarpered.

It's less than 48h since s/he was active in this thread.