Motor shield with IR remote

Hello peops,

I build the last couple of weeks A wireless communication between bluetooth on my android and my
bluetooth shield on the arduino uno R3. To use a button on my phone i could turn on a motor.
while my ''void loop'' function gets data from the bluetooth, i could easily turn on the motor with interrupts
The motor keeps spinning (clockwise) while i could recieve data from my blue (example to use other button
on my phone to spin the motor anti-clockwise) and i also let the RGB LED shine in colors i liked.
[bluetooth shield : https://iprototype.nl/products/arduino/shields/bluetooth-shield ]

Now i want to do it with some other components. I now have a motor shield
https://iprototype.nl/products/arduino/shields/arduino-motor-shield on the arduino uno R3
and i IR-remote
Gravity: IR Kit For Arduino - DFRobot
This is the following program for so far:
#include <Stepper.h>
#include "IRremote.h"

int recieve = 2;
IRrecv irrecv(recieve);
decode_results result;

#define EN1_PIN 3
#define DIR1_PIN 12
#define EN2_PIN 11
#define DIR2_PIN 13
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, DIR1_PIN, DIR2_PIN);

void setup() {
analogWrite(EN1_PIN, 255);
analogWrite(EN2_PIN, 255);
myStepper.setSpeed(150);
Serial.begin(9600);
irrecv.enableIRIn();
}

void loop() {
if (irrecv.decode(&result)){ //sensor ontvangt iets
translateIR();
irrecv.resume();
}
}
void translateIR()
{
switch(result.value){
case 0xFD00FF:
Serial.println("POWER ON");
myStepper.step(stepsPerRevolution);
break;
//more cases is unnecessary for now
}
} //everytime i have to use that ''power on'' button while it spins 1 or2 rotation and goes off
with interrupts i can do more things but the problem is now that interrupts or attachinterrupt
so far doesnt work like my other program some1 got a solution for me ????

Greetz Noddy1988

Ok, you know how move motors/stepper with interrupt and in your posted code you not use interrupts.
But you wrote a sketch. Work? Not compile? What's the problem? For me is not clear the problem, sorry.

with this function i call an interrupt

#define LED 13

void setup()
{
pinMode(LED, OUTPUT);

// initialize timer1
noInterrupts(); // disableallinterrupts
TCCR1A = 0;
TCCR1B = 0;

TCNT1 = 40536; // preload timer 65536-16MHz/64/ 10herts
TCCR1B |= (1 << CS11 | 1<<CS10); // 64prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enableallinterrupts
}

ISR(TIMER1_OVF_vect) // interrupt service routine thatwraps a user definedfunctionsuppliedbyattachInterrupt
{
TCNT1 = 40536; // preload timer
digitalWrite(LED, digitalRead(LED) ^ 1); <-- this will be the stepper NOT LED [ myStepper.step(stepsPerRevolution);
}

void loop()
{
// here i do IR remote [ to find hexidecimal value from the remote] i want to turn the motor
}

unfortunately this wont work because of the speed of the interrupt or speedrotation of the stepmotor
my question how can i have 2 threads??

You can't have 2 threads. There are some library that simulate the calling of multy threads:
http://www.leonardomiliani.com/2012/leos-un-semplice-so-per-arduino/?lang=en

You can work with stepper/motor without TIMER1 interrupt.
IRRemote library use timer1, so if you manipulate TIMER1 setting you can't use IRRemote. So don't work with stepper using interrupt.
With IDE there is a library to work with Stepper: Stepper - Arduino Reference

Yeah in my first post i actually used stepper.h..

but nevermind i have a solution

#include "IRremote.h"
int receiver = 10; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

int i = 0;
unsigned int counter = 64736;

void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
Serial.println("hallo");
irrecv.enableIRIn(); // Start the receiver
}
void loop(){
if (irrecv.decode(&results)) {
//Serial.println(results.value, HEX);
translateIR();
irrecv.resume(); // receive the next value
}
}

void translateIR()
{
switch(results.value)
{
case 0xFD00FF: Serial.println("POWER ON");
noInterrupts(); // disableallinterrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = counter; //
TCCR1B |= (1 << CS11 | 1<<CS10); // 64prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enableallinterrupts
break;
case 0xFDA05F: Serial.println("PLAY/PAUSE"); break;
case 0xFD40BF: Serial.println("Functie/stop");
TIMSK1 &=~ (1 << TOIE1); // enable timer overflow interrupt
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, LOW); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(3, 0); //Moves CH A
analogWrite(11, 0); //Moves CH B
break;
case 0xFD807F: Serial.println("VOL+"); break;
case 0xFD906F: Serial.println("VOL-"); break;
case 0xFD10EF: Serial.println("DOWN"); break;
case 0xFD50AF: Serial.println("UP"); break;
case 0xFD20DF: Serial.println("|<<"); break;
case 0xFD609F: Serial.println(">>|"); break;
case 0xFDB04F: Serial.println("EQ"); break;
case 0xFD708F: Serial.println("ST/REPT"); break;
case 0xFD30CF: Serial.println("0"); break;
case 0xFD08F7: Serial.println("1"); break;
case 0xFD8877: Serial.println("2"); break;
case 0xFD48B7: Serial.println("3"); break;
case 0xFD28D7: Serial.println("4"); break;
case 0xFDA857: Serial.println("5"); break;
case 0xFD6897: Serial.println("6"); break;
case 0xFD18E7: Serial.println("7"); break;
case 0xFD9867: Serial.println("8"); break;
case 0xFD58A7: Serial.println("9"); break;

}
}

ISR(TIMER1_OVF_vect) // interrupt service routine thatwraps a user definedfunctionsuppliedbyattachInterrupt
{
TCNT1 = counter; // preload timer
UpDate();
}

void UpDate(){
i++;
if(i<5){
switch(i){
case 1:
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
break;
case 2:
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
break;
case 3:
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
break;
case 4:
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
break;
}
}
else{
i=0;
}
}

When i use a button on my remote the motor shield turn on my stepmotor and with another button i turn in off
thanks for help anyway

Greetz Noddy1988

I have read that normally ISR() routine require a fast code (else you can loose some interrupt)
Your code update() is not long?
digitalWrite and analogWrite are "heavy", you can change using direct port manipulation (see internet, you can find many examples) but the code will be specific for a MCU like 328 (Uno) or 2560 (Mega)

Yes ISR have to go fast with short code, im just experimenting with combinations
i already had bluetooth(with android to bluetooth shield to turn on a microstepdriver 50.5-6 with stepdriver pretty cool)

now im try to do it with IR-remote control and motor shield
the program above works and it spins pretty fast (because i can change the frequentie now)
other program they use delay (i dont want delay, i want to do something else when its spinning)
if it spins like the frequentiei dont know i have to make some results in excel first.

i dont know what you mean by pin manipulation Arduino Reference - Arduino Reference ??
.. digitalx0 (RX) and digital1(TX) i cant use it. if i can it would be lovely cause i need 1 output more xD
(for display) to write the frequentie to display with liquidcrystal.h. i dont want to use my calculation everytime xD

Noddy1988:
i dont know what you mean by pin manipulation Arduino Reference - Arduino Reference ??

Yes, direct Port not Pin manipulation :blush: :grin:

Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;

aha yes i understand , i was forgot about that, now i know why I used digitalwrite/read (its easy to read)
i will make that shot code with /* comments */ //..

thank you