The following is the firmware code that can be used by 0816 feeder control,There are no changes in the hardware. Updating the firmware can support 0775 feeders.
const uint8_t Power_Control_Pin = A15; // Define power control pins
// Define motor control pins
const uint8_t Motor_Control_Pin[24] = {
13, 11, 9, 7, 5, 3,
14, 16, 18, 22, 24, 26,
30, 32, 34, 36, 38, 40,
42, 44, 46, 48, 50, 52
};
// Define feedback pins
const uint8_t Feedback_Sensor_Pin[24] = {
12, 10, 8, 6, 4, 2,
15, 17, 19, 23, 25, 27,
31, 33, 35, 37, 39, 41,
43, 45, 47, 49, 51, 53
};
uint8_t pinValue = 0; // Store Pin value
uint8_t pulseValue = 0; // Store impulse value
uint16_t cmdValue = 0; // Store command value
uint8_t rxCount = 0; // Used for serial port receiving count
char rxBuffer[200] = {0}; // Store data received by serial port
void setup()
{
Serial.begin(9600); // Initialize serial port, baud rate 9600
pinMode(Power_Control_Pin, OUTPUT); // Set the power control pin to output mode
digitalWrite(Power_Control_Pin, LOW); // Set the power control pin output low level
for (uint8_t i = 0; i < 24; i++) //
{
pinMode(Motor_Control_Pin[i], OUTPUT); // Set the motor control pin to output mode
digitalWrite(Motor_Control_Pin[i], LOW); // Set the motor control pin output low level
pinMode(Feedback_Sensor_Pin[i], INPUT_PULLUP); // Set the feedback pin to the pull-up input mode
}
}
void loop()
{
while (Serial.available()) // Cycle until no data is readable on the serial port
{
rxBuffer[rxCount] = Serial.read(); // Read one bit data
if (rxBuffer[rxCount] == '\n') // If the terminator is received
{
rxBuffer[rxCount] = 0; // Set packet Terminator
Serial.println(rxBuffer); // Print received data
Unpack_Data(); // Parse data
rxCount = 0; // Restart receiving data
}
else //
rxCount++; //
if (rxCount >= 200) // If the received data exceeds the size of the serial port buffer array
rxCount = 0; // Restart receiving
}
}
void Unpack_Data(void)
{
char *Tail = NULL, *Head = NULL; // Pointer to store parsing data
if ((Head = strchr(rxBuffer, 'M')) != NULL) // If M is found
{
if ((Tail = strchr(Head, ' ')) != NULL) //If a space character is found
{
*Tail = 0; //Truncate string
cmdValue = atoi(Head + 1); //
Serial.print(cmdValue); //
Serial.print(","); //
if (cmdValue == 610) // 610
{
if (strstr(Tail + 1, "S1") != NULL) // If S1 is searched
{
digitalWrite(Power_Control_Pin, HIGH); // Power on
Serial.print(1); //
}
else if (strstr(Tail + 1, "S0") != NULL) // If S0 is searched
{
digitalWrite(Power_Control_Pin, LOW); // Turn off the power
Serial.print(0); //
}
}
else if (cmdValue == 600) // 600
{
Head = strchr(Tail + 1, 'N'); // Find N
Tail = strchr(Head, ' '); // Find spaces
*Tail = 0; // Truncate string
pinValue = atoi(Head + 1); // Get pin number
Head = strchr(Tail + 1, 'F'); // Find F
pulseValue = atoi(Head + 1); // Get propulsion value
pulseValue = pulseValue / 4; // Convert to pulse number (4mm, 8mm, 12mm, 16mm, 24mm, using 1:30 reduction ratio turbine. Turn the vortex rod once, the gear advances 4mm,
//turn it twice, advances 8mm, and so on
pulseValue = pulseValue ? pulseValue : 1; // Convert to pulse number (2mm, using a 1:60 reduction ratio turbine. The scroll rod rotates once, and the gear advances 2mm)
Serial.print(pinValue); //
Serial.print(","); //
Serial.print(pulseValue); //
Send_Pulse(); // Send pulse
}
Serial.println(); // 打印调试信息
}
}
}
void Send_Pulse(void)
{
for (uint8_t i = 0; i < pulseValue; i++) // Cycle enough pulses
{
digitalWrite(Motor_Control_Pin[pinValue], HIGH); // Motor control pin output high level
delay(200); //Wait for 200ms
digitalWrite(Motor_Control_Pin[pinValue], LOW); // Motor control pin output low level
while (digitalRead(Feedback_Sensor_Pin[pinValue]) != LOW) // Cycle until the feedback pin is low level
;
}
}