LowPowerLab Forum

Hardware support => Moteino => Topic started by: max313 on September 16, 2014, 02:30:05 PM

Title: HELP for adapt arduino code to moteino
Post by: max313 on September 16, 2014, 02:30:05 PM
Hello, sorry for my english , i'm french.

I control a stepper motor with optical encoder ans arduino.

I have a problem with my moteino RFM69HW 433 Mhz, my arduino code working but I can't adapt with my moteino. I need help for this transcod.

My arduino sketch :


#define encoder_a 2 //keep this on and interrupt pin
#define encoder_b 3 //keep this on and interrupt pin
#define motor_step 4 //can be any pin
#define motor_direction 5 //can be any pin

#include <delay.h>
volatile long motor_position, encoder;

void setup () {
  //set up the various outputs
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
 
  // then the encoder inputs
  pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);
  // enable pullup as we are using an open collector encoder
  digitalWrite(encoder_a, HIGH);
  digitalWrite(encoder_b, HIGH);
 
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, encoderPinChangeA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  encoder = 0; //reseet the encoder position to 0
}

void loop() {
  //do stuff dependent on encoder position here
  //such as move a stepper motor to match encoder position
  //if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);// move stepper in reverse
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    delayMicroseconds(200); //_delay_us(200); //modify to alter speed
    motor_position++;
    encoder = 0; //encoder--;
  }
  else if (encoder < 0) {
    digitalWrite (motor_direction, LOW); //move stepper forward
    digitalWrite (motor_step, HIGH);
    digitalWrite (motor_step, LOW);
    delayMicroseconds(200); //_delay_us(200); //modify to alter speed
    motor_position--;
    encoder = 0; //encoder++;
  }
}

void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
    encoder--;
  }
  else{
      encoder++;
  }
}

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
    encoder--;
  }
  else {
    encoder++;
  }
}


I would be really happy to have a proper code moteino, THANK YOU
Title: Re: HELP for adapt arduino code to moteino
Post by: Felix on September 16, 2014, 06:38:46 PM
My guess is it would be hard to reproduce your issue without your actual hardware.
The transceiver library introduces a few constraints and assumptions which you have to understand and account for in your code.
The general rule of thumb is that existing code + RFM69 code is not the same as before and you have to make changes such that both your previous hardware and the radios will work optimally. And unfortunately there is no silver bullet to solving that issue all the time the same way. It's a bit of a steep learning curve.
Title: Re: HELP for adapt arduino code to moteino
Post by: max313 on September 17, 2014, 04:14:47 PM
Thank you for your reply, but I'm really a newbie and I can't translate my arduino code to work with the moteino, who can help me ??