// Include the necessary libraries
#include <Stepper.h>
#include <SevSeg.h>
SevSeg sevseg_stepper; // SevSeg object for stepper count display
SevSeg sevseg_input; // SevSeg object for input number display
const int stepPin = 10 ; // defines pins numbers in stepper motor
const int dirPin = 11 ;
Stepper stepper(201, stepPin, dirPin);
const int ON_RESET_PIN = A0; // define control pin
const int UP_PIN = A1 ;
const int DOWN_PIN = A2 ;
const int SENSOR_PIN = A3 ;
const int SYSTEM_PIN = A4 ;
const int SOLENOID_PIN = A5 ;
bool resetPressed = false; // Variable to track if the switch is pressed
int pipeCount = 0; // Variable to hold the count of pipes
int BundleCount = 0;
// shift register digit pins
const int D1 = 9;
const int D2 = 8;
const int D3 = 12;
const int D4 = 13;
int count = 0; // Global variable to hold the count
int increament_count = 1 ; // global variable to hold the increament count
int pipe_count=0 ; // global variable to hold the pipe count
unsigned long prevMillis = 0;
const long interval = 1000; // Time interval for each step in milliseconds
unsigned long prevDisplayMillis = 0;
const long displayInterval = 10; // Time interval for display updates
// Define shift register 1 pins
const int latchPin1 = 3;
const int clockPin1 = 2;
const int dataPin1 = 4;
// Define shift register 2 pins
const int latchPin2 = 6;
const int clockPin2 = 5;
const int dataPin2 = 7;
// Define shift register 4 pins
const int dataPin3 = A6;
// Define segment patterns for numbers 0-9
byte Segment_Pins[10] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
uint16_t Segment_Pins_1[10] = {
0b0000000011111100, // 0
0b0000000001100000, // 1
0b0000000011011010, // 2
0b0000000011110010, // 3
0b0000000001100110, // 4
0b0000000010110110, // 5
0b0000000010111110, // 6
0b0000000011100000, // 7
0b0000000011111110, // 8
0b0000000011110110 // 9
};
void setup() { // put your setup code here, to run once:
pinMode(stepPin,OUTPUT); // Sets the two pins of stepper motor as Outputs
pinMode(dirPin,OUTPUT);
stepper.setSpeed(60); // Adjust this value as needed - Set the speed of the stepper motor in RPM
// Set up shift register 1 pins
pinMode(latchPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
// Set up shift register 2 pins
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
// Set up shift register 4 pins
pinMode(dataPin3, digitalRead(OUTPUT));
byte numDigits = 2;
byte digitPins_stepper[] = {D1, D2}; // Digit pins for stepper count display
byte digitPins_input[] = {D3, D4}; // Digit pins for input number display
sevseg_stepper.begin(COMMON_CATHODE, numDigits, dataPin1, clockPin1, latchPin1, true, false, false); // Use a shift register
sevseg_stepper.setBrightness(90); // Set brightness (0-100)
sevseg_input.begin(COMMON_CATHODE, numDigits, dataPin2, clockPin2, latchPin2, true, false, false); // Use a shift register
sevseg_input.setBrightness(90); // Set brightness (0-100)
pinMode(ON_RESET_PIN , INPUT) ;
pinMode(UP_PIN , INPUT) ;
pinMode(DOWN_PIN , INPUT) ;
pinMode(SENSOR_PIN , INPUT);
pinMode(SYSTEM_PIN , INPUT);
pinMode(SOLENOID_PIN , OUTPUT);
Serial.begin(9600); // Initialize serial communication for debugging
pipe_count = 0; // Reset pipe count to 0
delay(1000); // Debounce delay
// Wait for the reset button to be pressed again
while (digitalRead(ON_RESET_PIN) == LOW) {
// Check if the up button is pressed
displayNumber2(pipe_count); // Display pipe_count
if (digitalRead(UP_PIN) == HIGH) {
pipe_count++; // Increment the pipe count
delay(300); // Debounce delay
}
// Check if the down button is pressed
if (digitalRead(DOWN_PIN) == HIGH ) {
pipe_count--; // Decrement the pipe count
if (pipe_count<=0){
pipe_count=0;
}
delay(300); // Debounce delay
}
}
// Save the final display value
int savedValue = pipe_count;
}
void loop() {
unsigned long currentMillis = millis();
// Refresh display every displayInterval milliseconds
if (currentMillis - prevDisplayMillis >= displayInterval) {
prevDisplayMillis = currentMillis;
displayNumber2(pipe_count);
displayNumber(count);
}
if (digitalRead(SENSOR_PIN)==HIGH && count < pipe_count ){
// Rotate stepper motor every interval milliseconds
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= interval) {
prevMillis = currentMillis;
stepper.step(100); // Rotate stepper motor 50 steps (45 degrees)
// Increment count each time the stepper motor rotates
count++;
if (count > 99) {
count = 0; // Reset count if it exceeds 99
}
}
}
else if(count>=pipe_count ){
digitalWrite(SOLENOID_PIN,HIGH);
Serial.println("solenoid "+ String(digitalRead(SOLENOID_PIN)));
Serial.println("system "+ String(digitalRead(SYSTEM_PIN)));
if (digitalRead(SYSTEM_PIN) == HIGH ){
digitalWrite(SOLENOID_PIN,LOW);
count=0;
}
}
}
void displayNumber2(int number) {
int tens = number / 10;
int ones = number % 10;
// Shift out the segment pattern for both digits
digitalWrite(latchPin2, LOW); // Activate latch for both digits
shiftOut(dataPin2, clockPin2, MSBFIRST, Segment_Pins[tens]); // Shift out segment pattern for tens digit
digitalWrite(latchPin2, HIGH); // Deactivate latch
digitalWrite(D3, LOW); // Activate digit 1
delayMicroseconds(1000);
digitalWrite(D3, HIGH); // Deactivate digit 1
digitalWrite(latchPin2, LOW); // Activate latch for both digits
shiftOut(dataPin2, clockPin2, MSBFIRST, Segment_Pins[ones]); // Shift out segment pattern for ones digit
digitalWrite(latchPin2, HIGH); // Deactivate latch
digitalWrite(D4, LOW); // Activate digit 2
delayMicroseconds(1000);
digitalWrite(D4, HIGH); // Deactivate digit 2
}
void displayNumber(int number) {
int tens = number / 10;
int ones = number % 10;
// Shift out the segment pattern for both digits
digitalWrite(latchPin1, LOW); // Activate latch for both digits
shiftOut(dataPin1 , clockPin1, MSBFIRST, Segment_Pins[tens]); // Shift out segment pattern for tens digit
digitalWrite(latchPin1, HIGH); // Deactivate latch
digitalWrite(D1, LOW); // Activate digit 1
delayMicroseconds(1000);
digitalWrite(D1, HIGH); // Deactivate digit 1
digitalWrite(latchPin1, LOW); // Activate latch for both digits
shiftOut(dataPin1, clockPin1, MSBFIRST, Segment_Pins[ones]); // Shift out segment pattern for ones digit
digitalWrite(latchPin1, HIGH); // Deactivate latch
digitalWrite(D2, LOW); // Activate digit 2
delayMicroseconds(1000);
digitalWrite(D2, HIGH); // Deactivate digit 2
if (number==pipe_count){
BundleCount++;
}
int hundred = BundleCount / 100;
int tens1 = (BundleCount % 100) / 10;
int ones1 = (BundleCount % 100) % 10;
// Shift out the segment pattern for three digits
digitalWrite(latchPin1, LOW); // Activate latch for three digits
shiftOut(dataPin3 , clockPin1, MSBFIRST, Segment_Pins_1[hundred]); // Shift out segment pattern for hundred digit
digitalWrite(latchPin1, HIGH); // Deactivate latch
shiftOut(dataPin3 , clockPin1 , MSBFIRST , 0b0000000000000100);
delayMicroseconds(1000);
digitalWrite(latchPin1, LOW); // Activate latch for three digits
shiftOut(dataPin3 , clockPin1, MSBFIRST, Segment_Pins_1[tens1]); // Shift out segment pattern for tens digit
digitalWrite(latchPin1, HIGH); // Deactivate latch
shiftOut(dataPin3 , clockPin1 , MSBFIRST , 0b0000000000000010);
delayMicroseconds(1000);
digitalWrite(latchPin1, LOW); // Activate latch for three digits
shiftOut(dataPin3 , clockPin1, MSBFIRST, Segment_Pins_1[ones1]); // Shift out segment pattern for tens digit
digitalWrite(latchPin1, HIGH); // Deactivate latch
shiftOut(dataPin3 , clockPin1 , MSBFIRST , 0b0000000000000001);
delayMicroseconds(1000);
}
void saveNumber() {
// Save the final number and stop blinking display
displayNumber2(pipe_count);
delay(1000); // Wait for a second to ensure stable display
}