Conveyor belt- with counters

Hello , I am trying to make a conveyor belt, I use arduino infrared sensors, one of the is used to activate 28byj48 motor and after a while it will stop, the idea is that with 2 other sensors one above the other, to differentiate a large object from a small one (when a small object passes only one sensor is activated, when a large object passes the 2 sensors are activated) the issue is that while the motor is running when the sensor senses the object the counter does not change ( I use 2 Seven-segment display, one for large items and one for small items) I can't get the code to have those 2 sections separately, can you help me? I leave you my code

#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 12;
Stepper myStepper(stepsPerRevolution,53,49,51,47);
byte sensor3=12;

int display7a[10]= {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
byte a=2;
byte b=3;
byte c=4;
byte d=5;
byte e=6;
byte f=7;
byte g=8;
int contador=0;

void puerto(int bits,int ini,int fin){
  for(int i=ini;i<=fin;i++)
  {
    digitalWrite(i,bitRead(bits,i-ini));
  }
}
byte sensor=9;
int display7b[10]= {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
byte h=22;
byte k=23;
byte l=24;
byte n=25;
byte m=26;
byte o=27;
byte p=28;
int contador2=0;
byte sensor2=10;

void setup() {
  myStepper.setSpeed(motSpeed);

 for(int i=a;i<=g;i++){
    pinMode(i,OUTPUT);
  }
 
  for(int j=h;j<=p;j++){
    pinMode(j,OUTPUT);
  }
 pinMode(sensor,INPUT);
 pinMode(sensor2,INPUT);
 pinMode(sensor3,INPUT);
 
}

void loop() {
  if(digitalRead(sensor3) == LOW){
    delay(100);
    myStepper.step(stepsPerRevolution);
    delay(5000);
    myStepper.step(0);
    delay(100);
   }
//
  if(digitalRead(sensor) == LOW && digitalRead(sensor2) == HIGH ){
      delay(100);
      while(digitalRead(sensor) == LOW && digitalRead(sensor2) == HIGH ); //Anti-Rebote
      delay(100);
      contador++;
      
    }

  if(contador>9){
      contador=0;  
  }
  if(digitalRead(sensor) == LOW && digitalRead(sensor2) == LOW ){
      delay(100);
      while(digitalRead(sensor) == LOW && digitalRead(sensor2) == LOW ); //Anti-Rebote
      delay(100);
      contador2++;

    }

  if(contador2>9){
    contador2=0;
    }
          
  puerto(display7a[contador],a,g);
  puerto(display7b[contador2],h,p);

   
}

Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring. Give links to components.

Do not use delay( ) in your sketches as it stops program execution.

hi, here is a image of the circuit, do you think that the delay is the problem? what should I put instead?

That image is not a schematic, also the resolution is too low.


    delay(100); 
    myStepper.step(stepsPerRevolution);
    delay(5000);

Tell us what you think is happening in these lines of code ?

In those lines, the sensor3 after being activate it will make the motor run during 5 seconds

delay(5000) stops code execution for 5 seconds.

i.e.
During this period other code is not executed.


To make the code step the motor in a non blocking fashion, enable a millis( ) based TIMER for 5 seconds, step the motor once, do other things . . . when the TIMER expires, stop stepping.

See if this sketch runs on your setup.

Note:

  • pin 13 is used as a heartbeat LED.
  • Pin 8 (going to GND) is used to make the 5 seconds of motor movement start.

Can you see that the motor moves and the heartbeat LED both operating at the same time after the switch on pin 8 is pressed ?


#include <Stepper.h>

//********************************************^************************************************
Stepper myStepper(2048, 53, 49, 51, 47);
//Stepper myStepper(2048, 2, 3, 4, 5);

//********************************************^************************************************
#define resetTime          millis()

#define ENABLED            true
#define DISABLED           false

#define CLOSED             LOW
#define OPENED             HIGH

const byte heartbeatLED  = 13;         //Pin13---[220R]---[A->|-K]---GND
const byte goSwitch      = 8;          //+5V---[50k Internal Pullup]---[Input Pin]---[Switch]---GND

bool moveFlag            = DISABLED;

byte lastGoSwitchState;

const int motorSpeed     = 12;
int stepCount            = 0;

//timing stuff
unsigned long currentTime;
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long steppingTime;


//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  myStepper.setSpeed(motorSpeed);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(goSwitch, INPUT_PULLUP);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //capture the current time
  currentTime = millis();

  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (currentTime - heartbeatTime >= 500)
  {
    //restart the TIMER
    heartbeatTime = resetTime;

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          s w i t c h   T I M E R
  //is it time to check the switches ? every 50ms
  if (currentTime - switchTime >= 1)
  {
    //restart the TIMER
    switchTime = resetTime;

    //go and check the switches
    checkSwitches();
  }

  //*************************************                          m o t o r   T I M E R
  //if enabled, is there time left to step the motor ?
  if (moveFlag == ENABLED && currentTime - steppingTime < 5000ul)
  {
    //move the motor 1 step
    moveStepper();
  }

  else
  {
    //disable motor movement
    moveFlag = DISABLED;
  }


  //************************************* 
  //other non blocking code goes here
  //************************************* 
  
} //END of   loop()


//********************************************^************************************************
void moveStepper()
{
  stepCount = stepCount + 1;

  myStepper.step(1);

  Serial.println(stepCount);

} //END of   moveStepper()


//********************************************^************************************************
void checkSwitches()
{
  //*********************************************                    g o S w i t c h
  //goSwitch code
  byte currentState = digitalRead(goSwitch);

  //**********************
  //was there a change in state ?
  if (lastGoSwitchState != currentState)
  {
    //update to the new state
    lastGoSwitchState = currentState;

    //**********************
    //if we are not moving, is the switch closed ?
    if (moveFlag == DISABLED && currentState == CLOSED)
    {
      //reset this TIMER
      steppingTime = resetTime;

      //enable the TIMER
      moveFlag = ENABLED;

      stepCount = 0;
    }

  } //END of   goSwitch code

} //END of   checkSwitches()


//********************************************^************************************************





I'm trying to adapt it to my code it doesn't work

Did you try the sketch from post #7 and just connecting to a circuit using pin 13 going to an LED (with resistor) to GND, pin 8 to a switch going to GND and you motor to 53, 49, 51, 47 ?

sorry men but I dont understand how it gets involve in the proyect. I wanna show you how I've been using millis, but anyway it has the same problem

#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 12;
Stepper myStepper(stepsPerRevolution,53,49,51,47);
byte sensor3=12;

boolean estadomotor=false;
int sensor3actual;
int periodo = 5000;  // tiempo para que el led se encienda
unsigned long tiempoAnterior = 0;  //guarda tiempo de referencia para comparar
  
int display7a[10]= {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
byte a=2;
byte b=3;
byte c=4;
byte d=5;
byte e=6;
byte f=7;
byte g=8;
int contador=0;

void puerto(int bits,int ini,int fin){
  for(int i=ini;i<=fin;i++)
  {
    digitalWrite(i,bitRead(bits,i-ini));
  }
}
byte sensor=9;
int display7b[10]= {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
byte h=22;
byte k=23;
byte l=24;
byte n=25;
byte m=26;
byte o=27;
byte p=28;
int contador2=0;
byte sensor2=10;

void setup() {
  myStepper.setSpeed(motSpeed);

 for(int i=a;i<=g;i++){
    pinMode(i,OUTPUT);
  }
 
  for(int j=h;j<=p;j++){
    pinMode(j,OUTPUT);
  }
 pinMode(sensor,INPUT);
 pinMode(sensor2,INPUT);
 pinMode(sensor3,INPUT);

}

void loop() {
  sensor3actual = digitalRead(sensor3);  
  if(digitalRead(sensor3) == LOW){
    //delay(100);
    //myStepper.step(stepsPerRevolution);
    //delay(5000);
    //myStepper.step(0);
    //delay(100);
    estadomotor= true;
    myStepper.step(stepsPerRevolution);
    tiempoAnterior=millis();
   }
   if((millis()-tiempoAnterior>=periodo)&&estadomotor==true){  //si ha transcurrido el periodo programado
      estadomotor=false;
      myStepper.step(0);
      
      }
//
  if(digitalRead(sensor) == LOW && digitalRead(sensor2) == HIGH ){
      delay(100);
      while(digitalRead(sensor) == LOW && digitalRead(sensor2) == HIGH ); //Anti-Rebote
      delay(100);
      contador++;
      
    }

  if(contador>9){
      contador=0;  
  }
  if(digitalRead(sensor) == LOW && digitalRead(sensor2) == LOW ){
      delay(100);
      while(digitalRead(sensor) == LOW && digitalRead(sensor2) == LOW ); //Anti-Rebote
      delay(100);
      contador2++;

    }

  if(contador2>9){
    contador2=0;
    }
          
  puerto(display7a[contador],a,g);
  puerto(display7b[contador2],h,p);

   
}

With the the Stepper.h library, myStepper.step(stepsPerRevolution); is blocking.

i.e. your sketch cannot do other things while the motor is moving to the destination.


Try the AccelStepper.h library.

Go through the examples before you add things to your project.

1 Like
  • You have a conveyor belt being moved by a 28byj48 motor.
  • sensor1 sits at a low level.
  • sensor2 sits at a high level
  • sensor3 enables the motor movement.

sensor 2 should sit just ahead of sensor1.

When sensor1 detects an object, you should check to see if sensor2 is detecting the object.
If both object sensors detect the object you want to increment a highObjectCounter.
If only the low sensor detects the object you want to increment a lowObjectCounter.


Install the AccelStepper.h library and try this sketch:


// https://forum.arduino.cc/t/conveyor-belt-with-counters/1041640/1

//********************************************^************************************************
// Version    YY/MM/DD    Description
// 1.00       22/10/14    Running sketch
// 1.01       22/10/11    Added some comments
//


#include <AccelStepper.h>

//ULN2003 Motor Driver Pins
//#define IN1                2
//#define IN2                3
//#define IN3                4
//#define IN4                5

#define IN1                53
#define IN2                49
#define IN3                51
#define IN4                47

#define FULLSTEP           4
#define HALFSTEP           8

#define ENABLED            true
#define DISABLED           false

AccelStepper myStepper(FULLSTEP, IN1, IN3, IN2, IN4);

const byte sensor1           = 9;
const byte sensor2           = 10;
const byte sensor3           = 12;
const byte heartbeatLED      = 13;

boolean motorFlag            = DISABLED;

byte lastSensor1SwitchState;
byte lastSensor2SwitchState;
byte lastSensor3SwitchState;

int contador1                = 0;
int contador2                = 0;

int display7a[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18};
byte a = 2;
byte b = 3;
byte c = 4;
byte d = 5;
byte e = 6;
byte f = 7;
byte g = 8;

int display7b[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18};
byte h = 22;
byte k = 23;
byte l = 24;
byte n = 25;
byte m = 26;
byte o = 27;
byte p = 28;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long motorTime;
unsigned long currentTime;

const unsigned long motorInterval = 5000ul;

//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  for (int i = 2; i <= 8; i++)
  {
    pinMode(i, OUTPUT);
  }

  for (int j = 22; j <= 28; j++)
  {
    pinMode(j, OUTPUT);
  }

  pinMode(heartbeatLED, OUTPUT);

  pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);
  pinMode(sensor3, INPUT_PULLUP);

  //*************************************
  //continuous rotation stepper movement
  myStepper.setMaxSpeed(500);
  myStepper.setSpeed(300);       //CW
  //myStepper.setSpeed(-300);    //CCW

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //capture the current time
  currentTime = millis();

  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (currentTime - heartbeatTime >= 500)
  {
    //restart the TIMER
    heartbeatTime = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          s w i t c h   T I M E R
  //is it time to check the switches ? every 50ms
  if (currentTime - switchTime >= 50)
  {
    //restart the TIMER
    switchTime = millis();

    //go and check the switches
    checkSwitches();
  }

  //*************************************                          m o t o r   T I M E R
  //if enabled, has this TIMER expired ?
  if (motorFlag == ENABLED)
  {
    if (currentTime - motorTime < motorInterval)
    {
      //must be executed each time through loop() for the motor to turn
      myStepper.runSpeed();
    }
    
    else
    {
      //turn off the motor
      motorFlag = DISABLED;
    }
  }

  //*************************************
  //other non blocking code goes here
  //*************************************

} //END of   loop()


//********************************************^************************************************
void checkSwitches()
{
  //*********************************************       s e n s o r 1
  //sensor1 code
  byte currentState = digitalRead(sensor1);

  //**********************
  //was there a change in state ?
  if (lastSensor1SwitchState != currentState)
  {
    //update to the new state
    lastSensor1SwitchState = currentState;

    //**********************
    //when the motor is turning, is this sensor detecting a new object ?
    if (motorFlag == ENABLED && currentState == LOW)
    {
      //is this sensor detecting an object ?
      if (digitalRead(sensor2) == HIGH)
      {
        contador1++;

        if (contador1 > 9)
        {
          contador1 = 0;
        }

        puerto(display7a[contador1], a, g);
      }

      else
      {
        contador2++;

        if (contador2 > 9)
        {
          contador2 = 0;
        }

        puerto(display7b[contador2], h, p);
      }

      Serial.print("contador1 = ");
      Serial.print(contador1);

      Serial.print("\t contador2 = ");
      Serial.println(contador2);
    }

  } //END of   sensor1 code

  //*********************************************       s e n s o r 3
  //sensor3 code
  currentState = digitalRead(sensor3);

  //**********************
  //was there a change in state ?
  if (lastSensor3SwitchState != currentState)
  {
    //update to the new state
    lastSensor3SwitchState = currentState;

    //**********************
    //if the motor is stopped, is this sensor detecting ?
    if (motorFlag == DISABLED && currentState == LOW)
    {
      //enable the motor TIMER
      motorFlag = ENABLED;

      //reset the TIMER
      motorTime = millis();
    }

  } //END of   sensor3 code

} //END of   checkSwitches()


//********************************************^************************************************
void puerto(int bits, int ini, int fin)
{
  for (int i = ini; i <= fin; i++)
  {
    digitalWrite(i, bitRead(bits, i - ini));
  }
  
} //END of   puerto()



1 Like

My friend I don't have word to express my gratitude, the code works amazing, seriously thank you. I want to ask you a last thing, the proyect works really good with the arduino connected to the pc but when I connected to a power source 12v the stepper turns on but It doesn't move, do you think that the code has something to do with that or it can be any other reason?

Show us a good image of your circuit wiring.

Confirm the stepping motor is rated at 12v, some are only 5v.


Go thru the sketch and try to understand what is going on.

Ask questions on the things you do not understand.

1 Like

the stepping motor is 5v but and for that reason it's connected to pin +5v the power supply of 12v is connected only in the arduino

well a question about the code can be

//is this sensor detecting an object ?
      if (digitalRead(sensor2) == HIGH)
      {
        contador1++;

I thought that the sensors that I use when sense an objet they will be en LOW

We really need to see a good image of your circuit wiring when not connected to the PC.


    //**********************
    //when the motor is turning, is this sensor detecting a new object ?
    if (motorFlag == ENABLED && currentState == LOW)
    {
      //is this sensor detecting an object ?
      if (digitalRead(sensor2) == HIGH)
      {
        contador1++;

. . . 

      else
      {
        contador2++;

This says:

  • When the conveyor is running and when the short IR sensor sees an object, and the high IR sensor does not see that same object, the object must be small so we will add 1 to the small counter (contador1).

  • the else means digitalRead(sensor2) must have been low, the object must be large, so we will then add 1 to the large counter (contador2).


To better document this section of code, these comments might make more sense to you:

    //**********************
    //when the motor is turning, is this sensor detecting a new object ?
    if (motorFlag == ENABLED && currentState == LOW)
    {
      //is the object small ?                          <-----<<<<
      if (digitalRead(sensor2) == HIGH)
      {
        contador1++;

        if (contador1 > 9)
        {
          contador1 = 0;
        }

        puerto(display7a[contador1], a, g);
      }

     //the object must be large                       <-----<<<<
     else
      {
        contador2++;

        if (contador2 > 9)
        {
          contador2 = 0;
        }
2 Likes

Wow men thank you, you've helpme so much, forget about the power suppy issue, it was a connetion mistake, I want to ask you a last question

  //continuous rotation stepper movement
  myStepper.setMaxSpeed(500);
  myStepper.setSpeed(300);       //CW
  //myStepper.setSpeed(-300);    //CCW

If I want to increase the speed, do I have to put higher values in those commands or only in one? what is the limit that I can put in the commands?

Try

500 is near the top speed for these 5v steppers, however, you can tune the speed to see what your motor can run at.

Let’s say you found at 700 the motor starts to fail; suggest you then reduce to 650 or 600 and see if the motor will work flawlessly.

2 Likes

do I have to change another thing to change the speed?, the motor does not move with those new values

My motors work up to 525 but the torque is poor at 550.

Play with the myStepper.setSpeed(300); //CW line of code to see what is the maximum you can get while holding the motors rotor with you fingers.

EDIT
Your 5V power supply for the motor should be at least 1 amp.

1 Like