Pages: [1]   Go Down
Author Topic: Interrupt my code [see reply 11]  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have two different codes. One uses a switch statement for 3 cases whereas I converted the first code into the second code to use "IF" statements.

What I have is an IR receiver that gives an input to my Arduino which then gives two outputs (step and dir). Step controls whether the stepper motor is on/off and dir controls left/right. I get the code working using the switch case, but when I tried using the If case nothing happens. The code uploaded just fine. No error. So I'm not sure what I did wrong.

What I am trying to do is to get the motor code working using the if statements, that way I can add in an additional if statement that will interrupt the motor code. So it would look something like this:

If button 1 is pressed, rotate 3600 degrees. If button 2 is pressed while still rotating, stop the motor.

Code:

#include <IRremote.h>

#define button1 16582903
#define button2 16615543
#define button3 16599223
#define RECV_PIN  10

IRrecv irrecv(RECV_PIN);
decode_results results;
long lReceived = 0 ;

#define DIR_PIN 2
#define STEP_PIN 3

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(RECV_PIN , INPUT);
  
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop(){
 if (irrecv.decode(&results)) {
    lReceived = results.value ;
      switch (lReceived) {
    case button1:
      digitalWrite (STEP_PIN, HIGH);
      digitalWrite (DIR_PIN, HIGH);
      //rotate a specific number of degrees
      rotateDeg(3600, .5);
      delay(1000);
      break;
    case button2:
       digitalWrite(STEP_PIN, LOW);
       digitalWrite( DIR_PIN, HIGH);
      break;
    case button3:
      digitalWrite( STEP_PIN, HIGH);
      digitalWrite( DIR_PIN, LOW);
      rotateDeg(-3600, .5);  //reverse
      delay(1000);
      break;
      }
    irrecv.resume(); // Receive the next value
  }
}

void rotateDeg(float deg, float speed){
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir);

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(usDelay);

    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(usDelay);
  }
}

Code:

#include <IRremote.h>

#define button1 16582903
#define button2 16615543
#define button3 16599223
#define RECV_PIN  10

IRrecv irrecv(RECV_PIN);
decode_results results;
long Received = 0 ;

#define DIR_PIN 2
#define STEP_PIN 3

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(RECV_PIN , INPUT);
  
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop(){
 if (irrecv.decode(&results)) {
    Received = results.value ;
    if (Received==button1){
      digitalWrite (STEP_PIN, HIGH);
      digitalWrite (DIR_PIN, HIGH);
      //rotate a specific number of degrees
      rotateDeg(3600, .5);
    }
    if (Received==button2){
       digitalWrite(STEP_PIN, LOW);
       digitalWrite( DIR_PIN, HIGH);
    }
    if (Received==button3){
      digitalWrite( STEP_PIN, HIGH);
      digitalWrite( DIR_PIN, LOW);
      rotateDeg(-3600, .5);  //reverse
    }
      }
    irrecv.resume(); // Receive the next value
  }


void rotateDeg(float deg, float speed){
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir);

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(usDelay);

    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(usDelay);
  }
}
« Last Edit: November 19, 2012, 06:08:16 pm by fydad » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (Received=button1){

You don't compare values with =, you assign values with it. You compare values with ==.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changed it but it still won't rotate.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changed it but it still won't rotate.
Could be an issue with the assumption that the button values are ints. Try changing the defines to const long ... or adding an L to the end of the button values.

Have you tried debugging the value received with Serial prints?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure If I understand the second part fully.

When we ran the code to test the button values, we received a hex format for our button which we then converted to decimal. As I said earlier, the motor runs using the values for the switch case statement, so I know the button values are right. We spent a lot of time figuring out why it wasn't working with hex format when we heard that Arduino needs it to be in decimal format. I'm 100% sure that we have right buttons, as well as numbers. I can try adding an "L" but I don't think it will change.

For the second part, you are asking that when I run the code, I go into serial monitor to see what values the IR sensor received correct?

edit: The "L" did not work.
« Last Edit: November 19, 2012, 04:14:03 pm by fydad » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11195
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changed it but it still won't rotate.

Post your code.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

same as the 2nd code in the first post. I modified it already.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For the second part, you are asking that when I run the code, I go into serial monitor to see what values the IR sensor received correct?

Yes. Print what you received and what you are testing it against to see why they are different. If they are not, then put something inside the if statements to verify the if statements are firing, etc. That's how you debug with the Arduino.
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Code:

 if (irrecv.decode(&results)) {
    [/quote]

you are not comparing this code... i dont understand
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

also why dont u use the stepper lib??? it would make so easy your code
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you are not comparing this code... i dont understand
When written like that the if statement will run if the return value of the condition is not 0.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I decided to go back to the  previous code since it was working. I now have this code but a different problem. I need to attach an interrupt to my code.

What I have is when I push a button, if analyzes the two cases. From there it goes to my void rotateDeg loop and grabs that function.

But I need to interrupt the void rotateDeg loop when another button is pushed by my IR sensor. In this case, specifically one single button. It rotates but won't stop. Do I need to attach the interrupt inside the void rotateDeg loop? And if so, how? There might also be a problem in my state functions. I was thinking when I press a button on my IR sensor, does the state change from low to high or high to low or does it just remain high? Help and thoughts appreciated.

Code:

#include <IRremote.h>

#define button1 16582903
#define button2 16615543
#define button3 16599223
#define RECV_PIN  10

IRrecv irrecv(RECV_PIN);
decode_results results;
long Received = 0 ;

#define DIR_PIN 2
#define STEP_PIN 3
volatile int state = HIGH;

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(RECV_PIN , INPUT);
 
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  attachInterrupt(10, stopp, HIGH);
}

void loop(){
 if (irrecv.decode(&results)) {
    Received = results.value ;
      switch (Received) {
    case button1:
      digitalWrite (STEP_PIN, state);
      digitalWrite (DIR_PIN, HIGH);
      //rotate a specific number of degrees
      rotateDeg(3600, .5);
      delay(1000);
      break;
    case button3:
      digitalWrite( STEP_PIN, state);
      digitalWrite( DIR_PIN, LOW);
      rotateDeg(-3600, .5);  //reverse
      delay(1000);
      break;
      }
    irrecv.resume(); // Receive the next value
  }
}

void rotateDeg(float deg, float speed){
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir);

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(usDelay);

    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(usDelay);
    }
}

void stopp()
{
  if (irrecv.decode(&results)) {
    irrecv.resume(); // Receive the next value
    Received = results.value ;
    if (Received == button2) {   
     state = !state;
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What I have is when I push a button, if analyzes the two cases. From there it goes to my void rotateDeg loop and grabs that function.

This random collection of words makes little sense. The if statement doesn't analyze stuff. There is a rotateDeg function. Nothing gets grabbed.

Quote
But I need to interrupt the void rotateDeg loop
There isn't a rotateDeg loop. There is a rotateDeg function. In that function you can call irrecv.decode() as often as you want to. If it returns true, return from the function.
Logged

Pages: [1]   Go Up
Jump to: