Hardware Initialization – Serial communication, motors, IR sensors, LED ring, servo motor, and color sensor are initialized for operation.
Block Detection & Dispensing – IR sensors detect blocks; once 10 blocks are counted, the dispenser and conveyor activate.
Color Sensing – The block moves under the color sensor, pausing briefly for color detection (red, blue, or green), triggering corresponding LED colors and servo adjustments.
Sorting Process – The conveyor positions the block for sorting, and sensor2 confirms passage before processing the next block.
Real-Time Optimization – Direct Port Manipulation ensures precise control of sensors and motors, bypassing digitalWrite()/digitalRead() for efficiency.
Modular Code Structure – Functions like steppergeardispensor(), spinconveyorbelt(), and color sensor() are structured outside the main loop for clarity and real-time performance.
// Stepper motor pins - set to output using DDRD for pins 6 and 7 (PORTD)
DDRD |= (1 << PD6) | (1 << PD7); // Set stepPin1 (PD6) and dirPin1 (PD7) as OUTPUT
DDRD |= (1 << PD4) | (1 << PD5); // Set stepPin2 (PD4) and dirPin2 (PD5) as OUTPUT
// IR sensor pins - set to input using DDRB for pins 8 and 9 (PORTB)
DDRB &= ~(1 << PB0) & ~(1 << PB1); // Set irSensor1Pin (PB0) and irSensor2Pin (PB1) as INPUT
-----------------------------------------------------------------------------
uint8_t sensor1State = PINB & (1 << PB0); // Read irSensor1Pin (PB0) using PINB
uint8_t sensor2State = PINB & (1 << PB1); // Read irSensor2Pin (PB1) using PINB
-----------------------------------------------------------------------------
void steppergeardispensor () {
// Set direction for stepper motor 2 to turn left (HIGH)
PORTD |= (1 << PD5); // dirPin2 HIGH (Set PD5 to HIGH to turn motor left)
for (int x = 0; x < 1000; x++) {
PORTD |= (1 << PD4); // stepPin2 HIGH (Set PD4 to HIGH to pulse stepPin2)
delayMicroseconds(500);
PORTD &= ~(1 << PD4); // stepPin2 LOW (Set PD4 to LOW to complete the pulse)
delayMicroseconds(500);
}
delay(2000);
Optimized for real-time performance using Direct Port Manipulation for fast sensor reading and motor control.