Hey guys, so I am fairly new to arduino, basically I have hooked up my arduino uno to the nema 17 stepper motor using the L298N driver and it is working, but I am unsure of what the code is where if I hold UP on the it remote it will move and when I left go it stops and also the same with down. Could I please get an example
thankyou
Sorry if I'm going to sound a little hazy on the details here- it's quite some years since I used a TV remote on an Arduino. I fear the sketch/es I had back then were probably lost 2-3 laptops ago.
First (assuming you have an ir receiver that is....) you need to install the ir remote library, then if my memory serves there's a sketch there which allows you to see what code is generated by each button of your particular remote. Make a note of such things, say it's xxxx for the "up".
Then in your actual code, you include the ir library and all the code from one of the examples to make it read, and in an "if" where you test for the code to be xxxx, set the output pins to the L298N according to what you want the motor to do.
Hey, so I've already done that but it only defines the button code when it is pressed, when it is held it says the actual code once then just repeats 'FFFFFFFFFFF', this is the same for every button when they are held. I am just wanting the motor to go forwards while I hold the button on the it remote as well as backwards.
Or if its easier, if theres a code for the buttons, up, down and stop. When up is pressed it runs until the stop button is pressed, the same thing as down. That way I don't need to find a code for when a button is pressed and depressed. Is that possible?
That would be like this very pseudo code I imagine:
if (code == xxxx) //up
set the pins to forward the l298
else if (code == yyyy) //down
set the pins to reversethe l298
else if (code == zzzz) //stop
set the pins to halt the l298
else
serial print a message to say unknown code
sorry, I dont understand what to put in code or in xyz. Although is there an alternative option for me to input another button from the IRremote to stop whatever code is running. This is what I have right now.
case 0xFFE01F: // UP button pressed
stepper.setSpeed(50);
Steps2Take = 2000; // Rotate CW
stepper.step(Steps2Take);
delay(2000);
break;
case 0xFF906F: // DOWN button pressed
stepper.setSpeed(50);
Steps2Take = -2000; // Rotate CCW
stepper.step(Steps2Take);
delay(2000);
break;
Please post your full code so we can have a look at it and adviose accordingly. Don't forget to use code tags (see How to get the best out of this forum that describes how to use code tags).
You will need a variable that holds the last result; you update that variable only if the result is not FFFFFFFFFFF. Your switch/case will use that variable.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project See About the IDE 1.x category.
#include <IRremote.hpp>
#include <Stepper.h>
/*----- Variables, Pins -----*/
#define STEPS 200 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
int receiver = 5; // Signal Pin of IR receiver to Arduino Digital Pin 6
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper stepper(STEPS, 8, 9, 10, 11);
IRrecv irrecv(receiver);
decode_results results;
void setup()
{
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFE01F: // UP button pressed
stepper.setSpeed(50);
Steps2Take = 2000; // Rotate CW
stepper.step(Steps2Take);
delay(2000);
break;
case 0xFF906F: // DOWN button pressed
stepper.setSpeed(50);
Steps2Take = -2000; // Rotate CCW
stepper.step(Steps2Take);
delay(2000);
break;
irrecv.resume(); // receive the next value
}/* --end main loop -- */
}
This is the basic idea
void loop()
{
static uint32_t btnCode;
if (irrecv.decode(&results)) // have we received an IR signal?
{
// if not a repeat code
if (results.value != 0xFFFFFF)
{
btnCode = results.value;
}
}
switch (btnCode)
{
...
...
}
...
...
}
The static variable will remember the last button; it will be set if the value is not the repeat code 0xFFFFFF.
Your switch/case will react on btnCode, not on value.
sorry im not sure where i would plug that into my code, would you be able to put in that code in the correct position in my code and send the whole thing?
sorry I'm so confused!
Just put your cases in the switch(btnCode).
You only must find the right place for irrecv.resume(); // receive the next value
as I think it's at the wrong place at this moment. I think that below should be the right place.
This is one of your cases
Screenshots of code and errors are useless.
It looks like you copied my loop() into your loop().
You have to replace your loop() by the loop() that I provided and next copy your cases into the code I provided.
Im still getting an error, this is the code I've done now.
#include <IRremote.hpp>
#include "Stepper.h"
/*----- Variables, Pins -----*/
#define STEPS 200 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
int receiver = 5; // Signal Pin of IR receiver to Arduino Digital Pin 6
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 9, 10, 11);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void loop()
{
static uint32_t btnCode;
if (irrecv.decode(&results)) // have we received an IR signal?
{
// if not a repeat code
if (results.value != 0xFFFFFF)
{
btnCode = results.value;
}
irrecv.resume(); // receive the next value
}
switch (btnCode)
{
case 0xFFE01F: // UP button pressed
small_stepper.setSpeed(50);
Steps2Take = 2000; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
break;
case 0xFF906F: // DOWN button pressed
small_stepper.setSpeed(50);
Steps2Take = -2000; // Rotate CCW
small_stepper.step(Steps2Take);
delay(2000);
break; // your cases here
}
...
...
}
Remove the dots, they are meant to indicate possible code that you have there ( I don't think you have anything there).
Now it just says error compiling for arduino uno
#include <IRremote.hpp>
#include "Stepper.h"
/*----- Variables, Pins -----*/
#define STEPS 200 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
int receiver = 5; // Signal Pin of IR receiver to Arduino Digital Pin 6
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 9, 10, 11);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void loop()
{
static uint32_t btnCode;
if (irrecv.decode(&results)) // have we received an IR signal?
{
// if not a repeat code
if (results.value != 0xFFFFFF)
{
btnCode = results.value;
}
irrecv.resume(); // receive the next value
}
switch (btnCode)
{
case 0xFFE01F: // UP button pressed
small_stepper.setSpeed(50);
Steps2Take = 2000; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
break;
case 0xFF906F: // DOWN button pressed
small_stepper.setSpeed(50);
Steps2Take = -2000; // Rotate CCW
small_stepper.step(Steps2Take);
delay(2000);
}
irrecv.resume(); // receive the next value
}
Where is your setup() function?
Where did you get the IRremote library? IRremote.hpp looks unfamiliar to me.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.