I am pretty green at this stuff. So if I do things wrong I will try to correct it, The following sketch is something I am try run but I get no response at the stepper. Today I added the stepper.runSpeed() at each CASE but still nothing.
#include <AccelStepper.h>
#include <IRremote.h>
#define One_button 0xE0036B40 // code received from one button
#define Two_button 0x594F978 // code received from two button
#define Three_button 0x4CB0FADC // code received from three button
#define five_button 0x75D18CF7 // code received from five button
int RECV_PIN = 11; //output pin of IR receiver to pin 11 of arduino
AccelStepper stepper(1, 3, 6);
IRrecv irrecv(RECV_PIN); //Arduino will take output of IR receiver from pin 11
decode_results output;
void setup()
{
// Serial Monitor @ 9600 baud
Serial.begin(9600);
// Enable the IR Receiver
irrecv.enableIRIn();
stepper.setMaxSpeed(1000);
}
void loop()
{
if (irrecv.decode(&output))
{
unsigned long value = output.value; // **** change to unsigned long
Serial.println(value, HEX);
switch (value)
{
case One_button:
stepper.setSpeed(1000);
stepper.runSpeed();
break;
case Two_button:
stepper.setSpeed(-1000);
stepper.runSpeed();
break;
case Three_button:
stepper.setSpeed(-6.202119556);
stepper.runSpeed();
break;
case five_button:
stepper.setSpeed(0);
stepper.runSpeed();
break;
}
irrecv.resume();
}
stepper.runSpeed();
}
type or paste code here
How stuck to AccelStepper are you? An alternative that I have been using is the MobaTools stepper library (available via the IDE library manager). I think easier to learn. When you do a move, you do not have to call the run(). Steps "in the background" so no blocking. Has acceleration, too.
Here is a simple demo using an IR remote to control stepper speed with the MobaTools stepper library. The rotate() function will step indefinitely in the background. I use a latest version of the IRremote library. This will probably not run with an older version of the library (<3.0).
#include <IRremote.h>
#include <MobaTools.h>
const byte IR_RECEIVE_PIN = 9;
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;
const int STEPS_REVOLUTION = 200;
MoToStepper stepper( STEPS_REVOLUTION, STEPDIR );
void setup()
{
Serial.begin(115200);
Serial.println("IR stepper speed control");
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
pinMode(enablePin, OUTPUT); // for CNC shield
digitalWrite(enablePin, LOW); // for CNC shield
stepper.attach( stepPin, dirPin );
stepper.attachEnable(enablePin, 0, 1);
stepper.setSpeed(500); // rpm /10, 100 = 50 RPM
stepper.setRampLen(10);
stepper.setZero();
}
void loop()
{
if (IrReceiver.decode())
{
unsigned long keycode = IrReceiver.decodedIRData.command;
Serial.println(keycode);
if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
{
IrReceiver.resume();
return;
}
IrReceiver.resume();
static int thisSpeed = 0; // holds the current speed
switch (keycode)
{
case 19: // fast fwd
thisSpeed = 1000;
stepper.rotate(1);
break;
case 16: // fast rev
thisSpeed = 1000;
stepper.rotate(-1);
break;
case 17: // really slow
thisSpeed = 6.202119556;
stepper.rotate(-1);
break;
case 7: //+100
thisSpeed += 100; // add 100 sps
break;
case 69: // -100
thisSpeed -= 100; // minus 100 sps
break;
}
stepper.setSpeed(thisSpeed);
}
}
Thank you. I am using this to turn the dome on my observatory. Another thing I have been running up against, I think, is, in an effort to use a reliable remote I am using an over the counter Universal remote. The hitch in that seems to a be different code every time a push the same key. Not sure how to get around that.
I have seen that several times and do not know the cause. Make sure thst the battery in the remote control is fresh. Put a 10K pullup resistor between the IR reciever data pin sn Vcc. That may help.
I pick up remote controls at the local thrift store for a dollar or two each. Tty a couple dirrerent ones.
instead of a IR-remote-control you could use two ESP8266 D1 mini boards which can send ESP-NOW-messages. They use 2,4 GHz RF so you don't even need a line of sight between sender an receiver.
I use this in my house as a remote door-opener. It works through 2 walls and a distance of 10 m
Power the motor off. When you press a key on the remote, the command code for that key will be shown in serial monitor.
unsigned long keycode = IrReceiver.decodedIRData.command;
Serial.println(keycode);
Copy the command code for each button that you wish to use and enter them, as shown, into each case statement. Enter the action to take for that key into the body of its case. Power the motor and test.
If you prefer to use the HEX values for the command codes, change the print to: Serial.println(keycode, HEX);
This has less to do with the above sketch and more todo with hardware performance. Why do my serial prints seem to take forever to present. Same board same laptop previously would display in near real time.
This, I've used it in the past fine. I suspect its the IR receiver. I've tried different boards and laptops to no avail.
#include <IRremote.h> //including infrared remote header file<br>
int RECV_PIN = 11; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results))
{
int value = results.value;
Serial.println(" ");
Serial.print("Code: ");
Serial.println(results.value); //prints the value a a button press
Serial.println(" ");
irrecv.resume(); // Receive the next value
Serial.println("*****************");
}
}
It has been 10 mins since the last monitor print. I have pressed the remote key several times in the interim. I press the key, the led brightens after a bit the led dims2, no monitor print and then I press the key again. ?????????
Bingo, a drop resistor in line of the 5V is doing the trick. Now if I can just solve the riddle of a different code with every key press of the same key I will be happy.