- 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()