Go Down

Topic: Interrupt my code [see reply 11] (Read 905 times) previous topic - next topic

fydad

Nov 19, 2012, 09:42 pm Last Edit: Nov 20, 2012, 12:08 am by fydad Reason: 1
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: [Select]


#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: [Select]


#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);
 }
}

Arrch

Code: [Select]

if (Received=button1){


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

fydad


Arrch


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?

fydad

#4
Nov 19, 2012, 10:11 pm Last Edit: Nov 19, 2012, 10:14 pm by fydad Reason: 1
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.

Go Up