09-08-2023, 12:50 AM
Code:
#include <RCSwitch.h>
#include "PCF8574.h"
#define motor_pin_1 0
#define motor_pin_2 1
#define motor_pin_3 2
#define motor_pin_4 3
RCSwitch mySwitch = RCSwitch();
PCF8574 pcf8574(0x24,5,4);
int Step_Dealy_Time=10;
int Turn_Step=2048;
int KEY1_BUTTON = 0;
int KEY2_BUTTON = 0;
int KEY3_BUTTON = 0;
int KEY4_BUTTON = 0;
int KEY5_BUTTON = 0;
void Read_buttonState ()
{
/*The following data is the remote control of five key values,You can replace them with your own remote key values*/
if(mySwitch.available()){
Serial.printf("mySwitch.getReceivedValue = %d\n",mySwitch.getReceivedValue());
if(mySwitch.getReceivedValue()==5756163) { KEY1_BUTTON=1;KEY2_BUTTON=0;KEY3_BUTTON=0;KEY4_BUTTON=0;KEY5_BUTTON=0;}//Clockwise_Turn
if(mySwitch.getReceivedValue()==5756172) { KEY1_BUTTON=0;KEY2_BUTTON=1;KEY3_BUTTON=0;KEY4_BUTTON=0;KEY5_BUTTON=0;}//Anti_Clockwise_Turn
if(mySwitch.getReceivedValue()==5756208) { KEY1_BUTTON=0;KEY2_BUTTON=0;KEY3_BUTTON=1;KEY4_BUTTON=0;KEY5_BUTTON=0;}//Stop_Turn
if(mySwitch.getReceivedValue()==5756364) { KEY4_BUTTON=1;KEY5_BUTTON=0;}//Speed up
if(mySwitch.getReceivedValue()==5756367) { KEY4_BUTTON=0;KEY5_BUTTON=1;}//Speed down
}
mySwitch.resetAvailable();
}
/************Clockwise************/
void Clockwise_Turn (int Step)
{
while (Step>0)
{
int which_one=Step % 4;
switch (which_one) {
case 0: // AB
pcf8574.digitalWrite(motor_pin_1, HIGH);
pcf8574.digitalWrite(motor_pin_2, HIGH);
pcf8574.digitalWrite(motor_pin_3, LOW);
pcf8574.digitalWrite(motor_pin_4, LOW);
break;
case 1: // BC
pcf8574.digitalWrite(motor_pin_1, LOW);
pcf8574.digitalWrite(motor_pin_2, HIGH);
pcf8574.digitalWrite(motor_pin_3, HIGH);
pcf8574.digitalWrite(motor_pin_4, LOW);
break;
case 2: //CD
pcf8574.digitalWrite(motor_pin_1, LOW);
pcf8574.digitalWrite(motor_pin_2, LOW);
pcf8574.digitalWrite(motor_pin_3, HIGH);
pcf8574.digitalWrite(motor_pin_4, HIGH);
break;
case 3: //DA
pcf8574.digitalWrite(motor_pin_1, HIGH);
pcf8574.digitalWrite(motor_pin_2, LOW);
pcf8574.digitalWrite(motor_pin_3, LOW);
pcf8574.digitalWrite(motor_pin_4, HIGH);
break;
}
Read_buttonState();
Button_speed_up_down();
if (KEY2_BUTTON == 1)
{
Anti_Clockwise_Turn(Turn_Step);
}
if (KEY3_BUTTON == 1)
{
Stop_Turn(Turn_Step);
}
Step--;
delay(Step_Dealy_Time);
}
}
/*******************Anti_Clockwise**********************/
void Anti_Clockwise_Turn (int Step)
{
while (Step>0)
{
int which_one=Step % 4;
switch (which_one) {
case 0: // AD
pcf8574.digitalWrite(motor_pin_1, HIGH);
pcf8574.digitalWrite(motor_pin_2, LOW);
pcf8574.digitalWrite(motor_pin_3, LOW);
pcf8574.digitalWrite(motor_pin_4, HIGH);
break;
case 1: //CD
pcf8574.digitalWrite(motor_pin_1, LOW);
pcf8574.digitalWrite(motor_pin_2, LOW);
pcf8574.digitalWrite(motor_pin_3, HIGH);
pcf8574.digitalWrite(motor_pin_4, HIGH);
break;
case 2: //BC
pcf8574.digitalWrite(motor_pin_1, LOW);
pcf8574.digitalWrite(motor_pin_2, HIGH);
pcf8574.digitalWrite(motor_pin_3, HIGH);
pcf8574.digitalWrite(motor_pin_4, LOW);
break;
case 3: //AB
pcf8574.digitalWrite(motor_pin_1, HIGH);
pcf8574.digitalWrite(motor_pin_2, HIGH);
pcf8574.digitalWrite(motor_pin_3, LOW);
pcf8574.digitalWrite(motor_pin_4, LOW);
break;
}
Read_buttonState();
Button_speed_up_down();
if (KEY1_BUTTON == 1)
{
Clockwise_Turn(Turn_Step);
}
if (KEY3_BUTTON == 1)
{
Stop_Turn(Turn_Step);
}
Step--;
delay(Step_Dealy_Time);
}
}
/*************Stop_Turn**********************/
void Stop_Turn (int Step)
{
pcf8574.digitalWrite(motor_pin_1, HIGH);
pcf8574.digitalWrite(motor_pin_2, HIGH);
pcf8574.digitalWrite(motor_pin_3, HIGH);
pcf8574.digitalWrite(motor_pin_4, HIGH);
Read_buttonState();
if (KEY1_BUTTON == 1)
{
Clockwise_Turn(Turn_Step);
}
if (KEY2_BUTTON == 1)
{
Anti_Clockwise_Turn(Turn_Step);
}
delay(Step_Dealy_Time);
}
/**************Speed up and down**********************/
void Button_speed_up_down()
{
if (KEY4_BUTTON == 1)
{
delay(50);
KEY4_BUTTON =0;
Step_Dealy_Time=Step_Dealy_Time-1;
if (Step_Dealy_Time<2)
{
Step_Dealy_Time=2;
}
}
if (KEY5_BUTTON == 1)
{
delay(50);
KEY5_BUTTON =0;
Step_Dealy_Time=Step_Dealy_Time+1;
if (Step_Dealy_Time>10)
{
Step_Dealy_Time=10;
}
}
}
void Button_Clockwise_or_Anti_Clockwise_Turn()
{
if (KEY1_BUTTON == 1&KEY2_BUTTON == 0&KEY3_BUTTON == 0)
{
Clockwise_Turn(Turn_Step);
}
if (KEY1_BUTTON == 0&KEY2_BUTTON == 1&KEY3_BUTTON == 0)
{
Anti_Clockwise_Turn(Turn_Step);
}
if (KEY1_BUTTON == 0&KEY2_BUTTON == 0&KEY3_BUTTON == 1)
{
Stop_Turn(Turn_Step);
}
}
void setup()
{
Serial.begin(115200);
mySwitch.enableReceive(digitalPinToInterrupt(16));
pcf8574.pinMode(motor_pin_1, OUTPUT);
pcf8574.pinMode(motor_pin_2, OUTPUT);
pcf8574.pinMode(motor_pin_3, OUTPUT);
pcf8574.pinMode(motor_pin_4, OUTPUT);
pcf8574.begin();
}
void loop()
{
Read_buttonState();
Button_speed_up_down();
Button_Clockwise_or_Anti_Clockwise_Turn();
delay(30);
}
KC868-A8M_28BYJ_48.zip (Size: 1.34 KB / Downloads: 158)