Show Posts
Pages: [1] 2 3 ... 12
1  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 26, 2013, 06:27:01 am
First thing's first: D10 should not be used when radio is in use.
Since I used it for the ED's enable pin, it messed everything up...
2  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 26, 2013, 02:47:57 am
Hey Robin2 - thanks. Your curiosity is my gain  smiley

The working code is using just one Moteino with encoder and stepper attached to it.
The changes in the encoder are translated to stepper movements.
 
Quote
  stepper.moveTo(encoderValue * stepsPerRotaryStep);
  stepper.run();

Obviously it works.

The 2nd code separates the encoder from the stepper.
"Sending moteino" has encoder attached to it, and it sends "up" or "down" via radio.
This is the "sending" code - reading the encoder:
Quote
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
      Serial.println ("Encoder --");
      Transmit(1);
    } 
    else {
      encoder0Pos++;
      Serial.println ("Encoder ++");
      Transmit(2);
    }
  } 
  encoder0PinALast = n;

This is the "sending" code - the transmission part:
Quote
int Transmit (int data)

  
  radio.Wakeup();
  radio.Send(GATEWAYID, payload, sendSize+(data));
  //Serial.println (encoder0Pos);
  Serial.println (payload[sendSize+(data)]);
  radio.Sleep();
}

And finally, the "receiving" unit - Moteino connected to stepper, supposed to translate radio to stepper movement:
Quote
void loop()
{

  if(encoderValue != lastencoderValue)
  {
    digitalWrite (enablePin, LOW);
    stepper.run();
    int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
    stepper.moveTo(encoderValue * stepsPerRotaryStep);
    int z = encoderValue * stepsPerRotaryStep;
    Serial.print ("encoderValue * stepsPerRotaryStep: ");
    Serial.println (z);
    Serial.print ("Stepper Pos: ");
    Serial.println (stepper.currentPosition());
    lastencoderValue = encoderValue;
    previousMillis = millis();
  } 
  else
  {
    //Stepper sleep after 5sec of no data
    unsigned long currentMillis = millis ();
    if (currentMillis - previousMillis>sleepTimer)
      digitalWrite (enablePin, HIGH);
  }

  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
      sum = 0;
      for (byte i = 0; i < radio.GetDataLen(); i++) //can also use radio.GetDataLen() if you don't like pointers
      {
        a = (radio.Data);
        
      }
      sum+=a;
      Serial.print("sum: ");
      Serial.println(sum);
      if (sum == 49)
      {
        encoderValue++;
        Serial.print("encoderValue: ");
        Serial.println(encoderValue);
      }
      else if (sum == 50)
      {
        encoderValue--;
        Serial.print("encoderValue: ");
        Serial.println(encoderValue);
      }
    }
  }
}
The result? The stepper hums and vibrates, and seems to be confused...
The strange behavior is also evident in these 2 facts:
1. once the encoderValue gets negative - Moteino LED shuts down...
2. Serial printing for debugging indicates that once encoderValue is negative, the stepperMoveTo() function is erratic.

This is very strange because the sum int is what it should be at all times...
Did I manage to explain the issue?
3  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 01:51:57 pm
bumping and hoping
4  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 11:12:52 am
Or maybe it's something to do with the pins? Something here I don't get?
5  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 10:01:24 am
Something isn't right here:
Code:
if (radio.CRCPass())
    {
      sum = 0;
      for (byte i = 0; i < radio.GetDataLen(); i++)
      {
        a = (radio.Data[i]);
        sum+=a;
      }
      Serial.print("sum: ");
      Serial.println(sum);
      if (sum == 49)
      {
        encoderValue++;

The way I'm trying to sum up the data isn't right but I don't really know what it is I'm messing up there.
6  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 09:51:20 am
Quote
What does the second code do? Do the motors move smoothly using that code?
The second code simply moves the stepper according to the encoder. Everything is attached to the same moteino.
Works like a charm.

Quote
What does the first code do? What is the radio stuff all about? You need to describe your project/problem in more detail.
Once the encoder is on one end (Sending unit) and the stepper is on the other (Receiving unit), I'm using the Moteino's radio
to send data - either up or down.
The data reaches the receiver just fine. I'm serial printing to make sure.
But something in the translation from the data to the actual movement is wrong.
7  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 09:34:30 am
Any thoughts? smiley-slim
8  Using Arduino / Programming Questions / Re: Using Moteino with encoder and stepper on: August 25, 2013, 07:13:28 am
Hi PaulS, thanks - here's the simple sketch. Works:

Quote
//////////////////////////////////////////////////////////////////
//Moteino + Optical Encoder + Stepper 
/////////////////////////////////////////////////////////////////
#include <RFM12B.h>
#include <avr/sleep.h>
#include <AccelStepper.h>

//encoder/motor/driver setup
int easyDriverMicroSteps = 4; //I think this is 4 or 8
int rotaryEncoderSteps = 75; //setps per rev on your encoder
int motorStepsPerRev = 200; //this is right  - steps per rev on the motor

int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down


// You will need to initialize the radio by telling it what ID it has and what network it's on
// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
// The Network ID takes values from 0-255
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID        2  //network ID used for this unit
#define NETWORKID    99  //the network ID we are on
#define GATEWAYID     1  //the node ID we're sending to
//#define ACK_TIME     50  // # of ms to wait for an ack

//encryption is OPTIONAL
//to enable encryption you will need to:
// - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
// - to call .Encrypt(KEY) to start encrypting
// - to stop encrypting call .Encrypt(NULL)
uint8_t KEY[] = "ABCDABCDABCDABCD";

//int interPacketDelay = 1000; //wait this many ms between sending packets
//char input = 0;
int data = 0;

// Need an instance of the Radio Module
RFM12B radio;
byte sendSize=0;
char payload[] = "1234567890";
bool requestACK=false;


int easyDriverDirPin = 8;
int easyDriverStepPin = 9;
int enablePin = 10;


//Sleep Function - to diable ED when not active
long previousMillis = 0;
int sleepTimer = 5000;

int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoderValue = 0;
long lastencoderValue = 0;

int encoder0PinALast = LOW;
int n = LOW;
AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);

void setup() {
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);
  pinMode(enablePin, OUTPUT);

  Serial.begin (115200);

  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  radio.Sleep(); //sleep right away to save power

  stepper.setMinPulseWidth(MinPulseWidth); //may need to play with this, but I think this is good.
  stepper.setMaxSpeed(50000);
  stepper.setAcceleration(1000000000); //try 100, or 1000 and see if it works
  stepper.setSpeed(50000); //play with this to see if it makes a diff (1 to 1000)



void loop() {
  int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
  stepper.moveTo(encoderValue * stepsPerRotaryStep);
  stepper.run();
  if(encoderValue != lastencoderValue)
  {
    lastencoderValue = encoderValue;
    digitalWrite (enablePin, LOW);
    previousMillis = millis();
  } 
  else
  {
    //Stepper sleep after 5sec of no data
    unsigned long currentMillis = millis ();
    if (currentMillis - previousMillis>sleepTimer)
      digitalWrite (enablePin, HIGH);
  }
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoderValue--;
      //Transmit(1);
      Serial.println (encoderValue);

    } 
    else {
      encoderValue++;
      //Transmit(2);
      Serial.println (encoderValue);

    }

  } 
  encoder0PinALast = n;
}  


int Transmit (int data)

  radio.Wakeup();
  radio.Send(GATEWAYID, payload, sendSize+(data));
  Serial.println (encoderValue);
  Serial.println (sendSize+(data));
  radio.Sleep();
}
9  Using Arduino / Programming Questions / Using Moteino with encoder and stepper on: August 25, 2013, 05:24:49 am
Hey guys.
I'm trying to adapt a code I've written for the Uno and XBee and import it to the Moteino.
In case you don't know what it is, here's a link http://lowpowerlab.com/moteino/smiley-small
The sending unit has an optical encoder that sends either "up" or "down" signals.
The receiving unit translates this into "encoder ++" or "encoder --" and than uses the AccelStepper lib to move the stepper in the right direction.

Now something is wrong there, and I can't for the life if me figure it out.
What happens is that the encoder values do change, but the motor seems to vibrate but not rotate.
I have checked the motor, the Easydriver, and the optical encoder.
I even began by simply doing a fresh start and writing a new code to test a Moteino unit with encoder and stepper attached to it.
It worked just fine.

I'd really appreciate if someone could have a look at this code and tell me what's wrong.
Thank you.
Quote
////////////////////////
//Moteino FF Receiver///
////////////////////////

#include <RFM12B.h>
#include <AccelStepper.h>
#include <avr/sleep.h>


//int for radio data
int a;
int sum;

//encoder/motor/driver setup
int easyDriverMicroSteps = 4;
int rotaryEncoderSteps = 75;
int motorStepsPerRev = 200;

int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down

int easyDriverStepPin = 8;
int easyDriverDirPin = 9;
int enablePin = 10;

volatile long encoderValue = 0;
byte dataReceive = 0;
long lastencoderValue = 0;

//ON LED
#define onLed 12

AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);

//Sleep Function - to diable ED when not active
long previousMillis = 0;
int sleepTimer = 5000;

// You will need to initialize the radio by telling it what ID it has and what network it's on
// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
// The Network ID takes values from 0-255
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID        1  //network ID used for this unit
#define NETWORKID     99  //the network ID we are on
#define GATEWAYID     2  //the node ID we're sending to
#define SERIAL_BAUD 115200

//encryption is OPTIONAL
//to enable encryption you will need to:
// - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
// - to call .Encrypt(KEY) to start encrypting
// - to stop encrypting call .Encrypt(NULL)
uint8_t KEY[] = "ABCDABCDABCDABCD";

// Need an instance of the Radio Module
RFM12B radio;
byte sendSize=0;
char payload[] = "1234567890";
bool requestACK=false;


void setup()
{
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);      //comment this out to disable encryption
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");

  stepper.setMinPulseWidth(MinPulseWidth);
  stepper.setMaxSpeed(50000);             //variable to later determine speed play/rewind
  stepper.setAcceleration(1000000000);
  stepper.setSpeed(50000);

  pinMode(enablePin, OUTPUT);
}

void loop()
{

  if(encoderValue != lastencoderValue)
  {
    digitalWrite (enablePin, LOW);
    stepper.run();
    int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
    stepper.moveTo(encoderValue * stepsPerRotaryStep);
    lastencoderValue = encoderValue;
    previousMillis = millis();
  } 
  else
  {
    //Stepper sleep after 5sec of no data
    unsigned long currentMillis = millis ();
    if (currentMillis - previousMillis>sleepTimer)
      digitalWrite (enablePin, HIGH);
  }

  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
      sum = 0;
      for (byte i = 0; i < radio.GetDataLen(); i++) //can also use radio.GetDataLen() if you don't like pointers
      {
        a = (radio.Data);
        sum+=a;
      }
      Serial.println(sum);
      if (sum == 49)
      {
        encoderValue++;
        Serial.println(encoderValue);
      }
      else if (sum == 99)
      {
        encoderValue--;
        Serial.println(encoderValue);
      }
    }
  }
}
10  Using Arduino / Interfacing w/ Software on the Computer / Re: Mega 2560 can't run "Dimmer" example on: March 09, 2013, 11:27:10 am
Anyone here was lucky enough to find a solution?
If not, I find this to be quite crazy that the Mega and processing simply do not perform well together and no one has addressed this problem  smiley-mad
11  Using Arduino / Interfacing w/ Software on the Computer / Re: "Dimmer" noob question on: March 09, 2013, 09:48:50 am
Searching some more I came upon this thread:
http://arduino.cc/forum/index.php?action=printpage;topic=52151.0
It seems to be a known issue, and I can't find a published solution.

I'm beginning to think the problem is in the pc side. i.e. something with the com address.

12  Using Arduino / Interfacing w/ Software on the Computer / Re: "Dimmer" noob question on: March 09, 2013, 09:20:47 am
Here's what I get:

Quote
Available serial ports:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
  • "COM1"
  • [1] "COM9"

And nothing happens...
What is going on here? Why is it that with the Nano it simply works, and with the Mega it's not?
13  Using Arduino / Interfacing w/ Software on the Computer / Re: "Dimmer" noob question on: March 09, 2013, 07:53:43 am
Quote
Is there no way for Processing to talk to Mega?
I was using a Mega. So, yes, Processing CAN talk to a Mega.

One thing I forgot to ask was which version of Processing you are using. I am using 1.5.1.

I did the same thing to the Arduino sketch, except that I added:
Code:
Serial.print("brightness: [");
Serial.print(brightness);
Serial.println("]");

And, I modified the Processing sketch, adding a serialEvent(Serial port) function, that Processing calls when there is sufficient serial data. I got the correct response in the Processing window, though I did have to read it one character at a time, casting to a character, in order to print it.

Using 1.5.1 as well.
So what else can be going on here?
Having a hard time figuring what you mean you added to the processing sketch. Could you post the code?

Thanks again!
14  Using Arduino / Interfacing w/ Software on the Computer / Re: "Dimmer" noob question on: March 09, 2013, 04:34:28 am
Just tested the sketches with my Nano - and it works!
Is there no way for Processing to talk to Mega?
15  Using Arduino / Interfacing w/ Software on the Computer / Re: "Dimmer" noob question on: March 09, 2013, 04:17:51 am
PaulS, just found this post by you:
Quote
I've confirmed that Processing does not talk to the Mega 2560
It's from this discussion :http://arduino.cc/forum/index.php?topic=79341.0
Is this the problem you think?
Pages: [1] 2 3 ... 12