// Pin configurations
const int startButtonPin = 2;          // Start button pin
const int photoSensorPin = 3;          // Photosensor pin
const int sevenSegmentPins[] = {4,5, 6, 7, 8, 9, 10, };  // 7-segment display pins
const int conveyorControlPin = 1;     // Conveyor control pin
const int remoteResetSwitchPin = 0;   // Remote reset switch pin

// Variables
volatile int boxCount = 0;
const int presetValue = 5;  // Set your desired preset value

void setup() {
  // Initialize pins
  pinMode(startButtonPin, INPUT_PULLUP);
  pinMode(photoSensorPin, INPUT);
  pinMode(conveyorControlPin, OUTPUT);
  pinMode(remoteResetSwitchPin, INPUT_PULLUP);
  
  for (int i = 0; i < 8; i++) {
    pinMode(sevenSegmentPins[i], OUTPUT);
  }

  // Attach interrupts
  attachInterrupt(digitalPinToInterrupt(photoSensorPin), photoSensorInterrupt, FALLING);
  attachInterrupt(digitalPinToInterrupt(startButtonPin), startButtonInterrupt, FALLING);
}

void loop() {
  // Check if preset value is reached
  if (boxCount >= presetValue) {
    stopConveyor();
    boxCount = 0;  // Reset box count
    delay(1000);   // Delay for stability
  }

  // Check if remote reset switch is pressed
  if (digitalRead(remoteResetSwitchPin) == LOW) {
    boxCount = 0;  // Reset box count
    startConveyor();
    delay(1000);   // Delay for stability
  }

  // Display box count on 7-segment display
  displayCount();
}

void photoSensorInterrupt() {
  // Increment box count when a box passes the photosensor
  boxCount++;
}

void startButtonInterrupt() {
  // Start conveyor when the start button is pressed
  startConveyor();
}

void startConveyor() {
  digitalWrite(conveyorControlPin, HIGH);  // Activate the conveyor motor
  Serial.println("Conveyor activated");
}

void stopConveyor() {
  digitalWrite(conveyorControlPin, LOW);   // Deactivate the conveyor motor
  Serial.println("Conveyor deactivated");
}

void displayCount() {
  // Display the box count on a 7-segment display
  // (You may need a library or specific code depending on the type of display)
  // For simplicity, let's assume a common anode 7-segment display
  for (int i = 0; i < 8; i++) {
    digitalWrite(sevenSegmentPins[i], (boxCount >> i) & 1);
  }
}
led-blue:A
led-blue:C
btn-red:1.l
btn-red:2.l
btn-red:1.r
btn-red:2.r
btn-green:1.l
btn-green:2.l
btn-green:1.r
btn-green:2.r
btn-blue:1.l
btn-blue:2.l
btn-blue:1.r
btn-blue:2.r
sevseg1:COM.1
sevseg1:COM.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:18
esp:19
esp:GND.1
esp:3V3.1
esp:3V3.2
esp:GND.2
esp:RST
esp:GND.3
esp:GND.4
esp:5V.1
esp:5V.2
esp:GND.5
esp:GND.6
esp:GND.7
esp:GND.8
esp:GND.9
esp:RX
esp:TX
esp:GND.10
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2