Hello !
I've been trying to control a stepper motor for a long time, but I have a problem with the code. Can you help me with the code please
#include < IRremote . h >
//Pin setting
const int EnablePin = 4 ; //Motor enable pin
const int StepPin = 3 ; //Motor PWM pin
const int DirectionPin = 2 ; // Motor direction pin
// Speed
const int FastMove = 200 ;
const int SlowMove = 1000 ;
// Remote control related
int RECV_PIN = 11 ;
IRrecv irrecv ( RECV_PIN );
decode_results results ;
uint16_t irCustomeCode ; //Custom ID (2 bytes)
uint8_t irValue ; // Actual button value
uint8_t irInvertValue ; // the inverted value of the button value
// value of the remote control button to use
const uint8_t IR_BACK_KEY = 0xE0 ;
const uint8_t IR_GO_KEY = 0xA0 ;
const uint8_t IR_REPEAT_KEY = 0xFF ;
const uint8_t NOTHING_KEY = 0 ;
const uint8_t BACK_KEY = 1 ;
const uint8_t GO_KEY = 2 ;
void setup ( )
{
// pin setup
pinMode ( EnablePin , OUTPUT ) ;
pinMode ( StepPin , OUTPUT ) ;
pinMode ( DirectionPin , OUTPUT ) ;
// Motor IC ON
digitalWrite ( EnablePin , LOW ) ;
Serial . begin ( 9600 ) ;
//Start the remote control
irrecv. enableIRIn ( ) ;
}
//Remote control key input function
uint8_t oldIrKey = NOTHING_KEY ;
uint8_t getIRKey ( )
{
uint8_t key = NOTHING_KEY ;
// If there is remote control data
if ( irrecv . Decode ( & results ) ) {
// custom ID and data separation
irCustomeCode = ( results . Value >> 16 ) & 0xFFFF ;
irValue = (results . value >> 8 ) & 0xFF ;
irInvertValue = results . value & 0xFF ;
irrecv . resume ( ) ;
//Check
if remote control data is normal if ( ~ irValue != irInvertValue )
{
// Output to key value monitor
Serial . println ( irValue , HEX ) ;
//If the key is a GO button,
if ( irValue == IR_GO_KEY )
{
key = GO_KEY ;
oldIrKey = key ;
}
//If the key is a BACK button
else if ( irValue == IR_BACK_KEY )
{
key = BACK_KEY ;
oldIrKey = key ;
}
//If the key is still pressed
else if ( irValue == IR_REPEAT_KEY )
{
// Transfer the previous button state
key = oldIrKey ;
}
else
{
//error
oldIrKey = 0 ;
}
}
else
{
//Error
oldIrKey = 0 ;
}
}
return key ;
}
void loop ( )
{
int i ;
//Get remote control button value
uint8_t irKey = getIRKey ( ) ;
//If the button is GO
if ( irKey == GO_KEY )
{
//High state moves clockwise
digitalWrite ( DirectionPin , HIGH ) ;
for ( i = 0 ; i < 50 ; i ++ )
{
//ON 1.8 1/4 move
digitalWrite ( StepPin , HIGH ) ;
//1ms(1000us) wait
delayMicroseconds ( SlowMove ) ;
//OFF 1/4 move of 1.8
digitalWrite ( StepPin , LOW ) ;
//1ms(1000us) wait
delayMicroseconds ( SlowMove ) ;
}
}
//If the button is Back,
else if (irKey == BACK_KEY )
{
//Low state moves counterclockwise
digitalWrite ( DirectionPin , LOW ) ;
for ( i = 0 ; i < 500 ; i ++ )
{
digitalWrite ( StepPin , HIGH ) ;
//0.2ms(200us) wait
delayMicroseconds ( FastMove ) ;
digitalWrite ( StepPin , LOW ) ;
//0.2ms(200us) standby
delayMicroseconds( FastMove ) ;
}
}
}