Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Convert frequency to delay formulae on: June 25, 2013, 08:26:07 am
Thank you very much for the info. Formulae for frequency works now.

Can you provide, please, more on counting the execution of the loop? I should be using millis() ? Can you please provide code?

And can someone tell me how to use delayMicroseconds() for higher frequencies?

Thank you.
2  Using Arduino / Programming Questions / Convert frequency to delay formulae on: June 25, 2013, 04:14:00 am
Hello, I am having some trouble commanding a stepper. I use two wires (direction and step = sensPin and tactPin), a variable used for delay (pause) and a variable used for frequency (_freq). The frequency (like direction and number of steps) is provided via serial communication. All these variables are integers, the ones provided via serial are processed using atoi function. This is my code:

Code:
   //  command the stepper
    stepsDone = 0;
    pause = (1/_freq)*1000;
    Serial.print(" pause: ");
    Serial.println(pause);
    //  sens
    digitalWrite(sensPin, _dir);
    //  tact
    while (stepsDone <= _steps) {
      digitalWrite(tactPin, LOW);
      digitalWrite(led, LOW);
      delay(pause);
      digitalWrite(tactPin, HIGH);
      digitalWrite(led, HIGH);
      delay(pause);
      stepsDone++;
      if (Serial.available() > 0) {
        digitalWrite(tactPin, LOW);
        digitalWrite(led, LOW);
        stepsDone = 0;
        break;
      }
    }

The turning led on and off is the check of this program. The problem is that conversion formulae ( pause = (1/_freq)*1000;) it's not working properly. If I am providing the value 1 for frequency (that means 1 step by one second), the pause variables works fine and receives the value of 1. The led blinks properly by 1 second. If I am providing value 2 (or other value) for frequency, the pause variable gets 0, I don't know why, because it should get 500 (that's half of a second because frequency 2 means 2 steps in a second). I have tried using float and then parse, but I get the same, 0.00 and 0.

The if (Serial.available) is just used for stopping the stepping, but I don't see any harm in using it. All code seems right, but it doesn't working. My second question is how to use delayMicroseconds() for higher frequencies? Should I use an if statement or just use this function instead delay(), but I have read in the specification that it can not handle properly values higher than 16383.

Thank you.
3  Using Arduino / Programming Questions / Stepper library - continuous stepping on: June 04, 2013, 01:33:03 pm
Hello, I've been trying to create a continuous rotation with Stepper library, but no luck. The command (is sent over serial connection - right now I am using Serial Monitor) is in this format: <dir, speed, steps> or dir can be 0 or 1 (direction). So, if I use <1,60,200> I get 200 steps at 60 rpm in 1 direction (ccw or cw, no mather).

I was thinking, why not make continuous rotation until it receives another command over the serial connection? So I use 0 for steps like a code message for this. That means <1,60,0> should make the stepper run continuously at 60 rpm in 1 direction. And when I send the stop message, (steps = 1), like <1,60,1> the stepper should make just one step and then stop. But it doesnt work and dont know why, please help. Thanks in advance.

Code: http://pastebin.com/Qz9S2UHd

or

https://gist.github.com/anonymous/5708849

or bellow.

Thank you!

Code:

//  libraries
#include <Stepper.h>

//  buffer containing the data
char inData[50];
//  index used for buffer
byte index;
//  set start and end of packet of data sent
#define SOP '<'
#define EOP '>'
//  and it's semaphore lights
bool started = false;
bool ended = false;
//  led
int led = 13;
//  the 3 variables of interest (default values)
int _dir = 0, _speed = 60;
unsigned int _steps = 1;
//  the stepper
Stepper myStepper(_steps, 7,8);


//  setup
void setup(){
  myStepper.setSpeed(_speed);
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  memset(inData, '\0', 50);
}


//  loop
void loop() {
  
  readSerialData();
  processPacket();
  
  if (_dir == 0 && _speed == 0 && _steps == 0)
  {
    digitalWrite(led, HIGH);
    Serial.println("arduino found");
  }
  else
  {
    digitalWrite(led, LOW);
    //  Print check for values
    Serial.print("direction: ");
    Serial.print(_dir);
    Serial.print(" speed: ");
    Serial.print(_speed);
    Serial.print(" steps: ");
    Serial.println(_steps);
    
    //  command the stepper
    //  continuous rotation (_steps == 0)
    if (_steps == 0)
    {
      myStepper.setSpeed(_speed);
      if (_dir == 0)
      {
        while (_steps != 1)
        {
          myStepper.step(1);
          readSerialData();
          processPacket();
          resetPacket();
        }
      }
      else
      {
       while (_steps != 1)
        {
          myStepper.step(-1);
          readSerialData();
          processPacket();
          resetPacket();
        }
      }
    }
    //  explicit rotation (_steps == other value than 0)
    else {
      myStepper.setSpeed(_speed);
      if (_dir == 0)
      {
        myStepper.step(_steps);
        delay(500);
      }
      else
      {
        myStepper.step(-_steps);
        delay(500);
      }
    }
  }
  
  resetPacket();
  
}

//  saving serial data into buffer
void readSerialData()
{
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();
    
    if (inChar == SOP)
    {
      index = 0;
      inData[index] = '\0';
      started = true;
      ended = false;
    }
    else if (inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if (index < 49)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
}

//  process the packet
void processPacket()
{
  //  check packet
  if (started && ended)
  {
    //  find _dir, _speed and _steps
    char * pch;
    pch = strtok (inData, ",");
    if (pch != NULL)
    {
      _dir = atoi(pch);
      _speed = atoi(strtok(NULL, ","));
      _steps = atoi(strtok(NULL, ","));
    }
  }
}

//  reset for the next packet
void resetPacket()
{
  started = false;
  ended = false;
  index = 0;
  inData[index] = '\0';
  //  _steps = 1;
}
4  Using Arduino / Programming Questions / Serial Communication Problem on: June 04, 2013, 11:07:45 am
Hello, I have an interface written in c#. The full c# code and arduino code may be found in the attached archive.

The program is simple: it sends via serial conection a message to arduino that tells it how to command a stepper motor. The message format is something like this: <0,60,200>, where 0 stands for direction (cw - 0 or ccw - 1), 60 for the speed and 200 for the number of steps. I have included a "check for arduino port" message that is <0,0,0>. When you press "conexiune" (conection) button and then scan (scanare) for ports, the application will send this message and receives (if any Arduino) the message "arduino found".

I have included a led check also. From serial monitor works fine.

The problem is with the interface: after sending the check message <0,0,0> from it and then a normal message like <1,20,300> the application (scan for port, led blink and also the command) wouldnt work anymore. It's like the application is getting stuck when receiving the confirmation "arduino found" message. Can you guys pritty please help me? Thank you in advanced.

PeterH: nope.
5  Using Arduino / Motors, Mechanics, and Power / Re: Anyone ever use one of these stepper drivers? Ebay 2M542 on: May 29, 2013, 05:21:07 am
Quote
1. I can't test everyday the driver and the stepper, because they are in a lab where I do not have access. That means I have to get things right without testing.

Frustrating (to say the least).  Depends what you need to test but using an ultra-cheap miniature stepper from eBay
might be a way forward.

[ Its a good discipline to program with the aim of getting it to work first time its tested  smiley ]

I quote from my self:

Quote
Arduino commands correctly other stepper with other drivers (same code).
]
That means that Arduino with the same code, an cheap driver and a scrap stepper from an old CD-ROM make the correct steps required. The 2M542 + turning table doesn't.
6  Using Arduino / Motors, Mechanics, and Power / Re: Anyone ever use one of these stepper drivers? Ebay 2M542 on: May 27, 2013, 05:01:36 am
Mark, ty very much.

BTW guys, I have a serious problem with this driver. I won't start another topic on this to keep forum clean, but please help. These are the details:

1. I can't test everyday the driver and the stepper, because they are in a lab where I do not have access. That means I have to get things right without testing.
2. Board is Arduino Leonardo.
3. Driver is, as mentioned above, 2M542. http://www.hi-end.ro/pdf/motoare/CMP5042.pdf
4. Driver settings are (watch the image: http://img2.dib-bid.com/gallery/cnc-motor-driver-2m542_03.jpg ) 1.0 A, 400 steps per revolution, Sw4: off (half-current)
5. Stepper is a big 1A turning table, 1.8 degree step. That means 200 steps per revolution.
6. Arduino commands correctly other stepper with other drivers (same code). Driver 2M542 works correctly the turning table, with other input. All wiring is good.
7. Code for Arduino is here: https://gist.github.com/fulminator/5656149 , but that's beyond of my questions. It's suffice to know that I try to control the stepper with Stepper.h library with this commands:  myStepper.step(_steps); delay(500); where myStepper is myStepper(200, 7,8); , 200 means that are 200 steps by revolution. Speed is 60.

Problem:
stepper doesn't make correct number of steps. I give it to make 400 steps, but it doesn't make the full rotation; it turn like less than a quarter or a quarter of revolution. Then I tried to give 1 step, but no motion was detected. I give it 10 steps and the turning table made only 1 step or so. I believe there is a non-syncronize between Arduino and driver, and not between driver and stepper, because driver and stepper works correctly with other input. The Arduino supplies the 400 steps for the one revolution, but somewhere in between is not recognized as 400, more like 40. Probably because of the library?

Question:
1. What's wrong?
2. Forget the code and the problem. Can you please supply a correct code? I hope the author of this topic would see this post.

Thanks in advanced and God bless. I have finish until Thursday.
7  Using Arduino / Motors, Mechanics, and Power / Re: Anyone ever use one of these stepper drivers? Ebay 2M542 on: May 23, 2013, 12:51:51 am
thank you.

One more question, you said that those inputs are an optocuplor with built-in resistors. That means that my Arduino board is fully protected, or should I take more precautions? If so, what precautions should I take? Another serial resistor on board pins that are used?
8  Using Arduino / Motors, Mechanics, and Power / Re: Anyone ever use one of these stepper drivers? Ebay 2M542 on: May 22, 2013, 12:42:03 pm
Can someone please give me some examples of wiring from this driver to Arduino?
9  Using Arduino / Programming Questions / Re: Arduino Stepper Control via Serial Command on: April 19, 2013, 05:23:01 pm
Thank you both, especially to PaulS. This is my final code: https://gist.github.com/fulminator/5423613

It's working great.

It's modified with both SOP EOP included. I will not include ',' between different data because it's unnecesary: speed and steps will never be bigger than, let's say, 999. So I can send 023 if I want only 23 steps; this way I will have a fixed 3 char length.

I have found out why stepper wouldn't start. It wasn't because memory overwriting at temporary char arrays declarations. It was because of memory overwriting at memset from the setup().

I will start building the c# application for sending the data. Any other help, hint and tips are welcome.

Thank you! And all have a good night tonight. It's 1:30 in the night for me right now.

10  Using Arduino / Programming Questions / Re: Arduino Stepper Control via Serial Command on: April 19, 2013, 04:33:52 pm
Thank you. I actually was thinking of how to send correctly data via serial. One more question on serial data: you can see that I have to send a boolean value (direction) a 0 -> 99 numeric value (speed) and a 0 -> 999 numeric value (steps). If direction and speed can be send and received as bytes, step cannot because it can be bigger than 255, maximum value of a byte. (I always tought that sending in bytes is ideal). That's why I have used the string approach: just send all data in one string (or char array) and sort it after. My questions is: did I did good? Or there is other better method?

And now, let's get to the issue. I have mentioned that reading the data works fine. I have send trough serial monitor strings like 060100 and got the correct results back: direction=0 speed=60 and steps=100. Not the serial communication is my issue. It's the stepper. It won't start.
11  Using Arduino / Programming Questions / Arduino Stepper Control via Serial Command on: April 19, 2013, 03:38:20 pm
Hello, I have a very strange issue. I have an Arduino Leonardo, a L293D driver (but that can be anything), and a scrap 4 wire stepper motor.

I am trying to control the motor trough serial (I am using serial monitor for now). So, I am sending 3 variables: sens (direction), viteza (speed), pasi (steps) trough serial. I am reading it as a char array and then formating them and print them (and it's working this far). If I will send 060100 trough serial I will have 0 for direction, 60 for speed and 100 for steps. If I will send 130256 then: sens=1; viteza=30; pasi=256. So this is working. Then, I am trying to setSpeed(viteza) and make myStepper.step(pasi) if sens=0 or else myStepper.step(-pasi). This is the part that doesn't works.

It's very important to say that stepper_oneRevolution example (modified accordingly: myStepper(pasi, 7,8)) WORKS JUST FINE.

I have tried:
  • instead of pasi=0 at declaration, I've put pasi=200
  • make pasi a constant int and give it 200
  • put a myStepper.setSpeed(60) in the setup()
  • put the print and comand blocks outside the if(serial.available) block
  • somebody on irc told me I haven't allocated enough space for the temporary char arrays used for formating data, so I added them +1 buffer[7] vitezaArr[3] and pasiArr[4], though the program runned fined without this surplus
  • I have put myStepper.step(200) first line in the loop() AND STILL NO RESULT

This is becoming very frustrating. Please help ASAP, I guess it's a very minor bug that I can't probably see right now. Thanks in advance. Here is my full code: https://gist.github.com/fulminator/5422506

edit: after the prints block there is a myStepper.step(pasi); line. Ignore it, it was only for testing.
edit2: corrected the number of wires of the stepper. 2 wires for Arduino: pin 7 and 8.
12  Using Arduino / Programming Questions / Re: Sending a string via serial communication Leonardo on: March 07, 2013, 03:02:34 am
I wonder what you would get if you echo out what you put in?

Code:
while (Serial.available() > 0) {
      incomingString[i++] = (char)Serial.read();
      Serial.print(incomingString[i]);
      delay(10);
  }

Maybe your if statements dont like double quotes, try single quotes.

squares. I get simple squares.

Can somebody please show me a clear example of SENDING and RECEIVING CHAR ARRAYS please? I got stuck at this problem and I can't go forward. Thanks!
13  Using Arduino / Programming Questions / Re: Sending a string via serial communication Leonardo on: March 05, 2013, 07:33:34 am
You can clearly see from the code that I am comparing the 'ican2' with no \0 character.  With serial monitor i have been sending with and without the end vharacter but no results. Please help I do not have any succes with this, the same with integers.
14  Using Arduino / Programming Questions / Re: Sending a string via serial communication Leonardo on: March 04, 2013, 03:54:28 pm
This is the correct link, I do not see why it isn't working: https://gist.github.com/fulminator/5084117
This is the code, anyways:

Code:
const int ledPin = 13; // the pin that the LED is attached to
char incomingString[10];    
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // see if there's incoming serial data:
  int i = 0;
  while (Serial.available() > 0) {
      incomingString[i++] = (char)Serial.read();
      delay(10);
  }
  
    // turn on the LED:
    if (incomingString == "Ican2") {
      digitalWrite(ledPin, HIGH);
    }
    // turn off the LED:
    if (incomingString == "Ican22") {
      digitalWrite(ledPin, LOW);
    }
  
}

I just can't make this work. I am sending the Ican2 or Ican22 string, RX led is liting, but the led is not get put on HIGH or LOW.
15  Using Arduino / Programming Questions / Sending a string via serial communication Leonardo on: March 04, 2013, 01:51:49 pm
Hello, guys, I have an Arduino Leonardo Board.

Today I've been playing with different data format sent and received via serial USB communication.

Based on PhysicalPixel example, I have succesefully sent data: bytes and chars.

But I cannot make use of char array. This is my code: https://gist.github.com/fulminator/5084117
After asking on mIRC, a fellow give me this and tried with nada results: http://pastebin.com/Q51VzU9J
It is funny, because i'ts looking correct, but it doesnt work. I have tried sending the "Ican2" from a c# GUI and from Serial Monitor. The ratebaud is the same everywhere: 9600. The  RX led from the board is liting like is suppose to when receiving data.

Help?

btw, I've just tested integers. The same as char arrays.
Pages: [1] 2 3