Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: 2 Stepper motors arrive at same time on: May 27, 2014, 03:54:17 pm
for very specific cases (x 12122 steps and y 43321 steps) you'll indeed get crazy results.

I don't see anything special about those numbers - why would they cause a problem?

...R
racemaniac is right, 12122 and 43321 gives a 525137162 lcm, that means 525137162 loops..thats why i have such slow speeds also there is no way i can think that i can keep the speed stable no matter what the LCM calculation is..

PaulS thanx for your answer again but my problem is not on getting the data from serial, the data from serial are correct and im pretty sure about that, also because i havent send any "tag" with more than 5 characters i havent seen the mistake when i "empty" the arrays.

So back to the original problem, i can rewrite the code as Robin2 and make it more efficient, but i believe  that with the LCM method i will never get the results i want.
2  Using Arduino / Programming Questions / Re: 2 Stepper motors arrive at same time on: May 26, 2014, 02:44:31 am
Thanx for your time.
Here is my complete code
Code:
#include <Servo.h>


////////////////// Stepper ///////////////

int stepper1dir = 12;
int stepper1step = 13;

int stepper2dir = 11;
int stepper2step = 10;


int Speed = 0;
int maxSpeed = 500;
int nSpeed = 500;

float stepSize = 0.113882;
float stepsX = 0;
float stepsY = 0;
float prevX = 0;
float prevY = 0;
float x = 0;
float y = 0;

unsigned long LCM = 0;

int moduloX = 0;
int moduloY = 0;
int mX = 0;
int mY = 0;
//////////////////////////////////////////


////////////////// Serial ///////////////

#define tagLength 6      // each tag ID contains 6 bytes
#define hash 0x23        // "#" hash
#define semicolon 0x3b   // ";" semicolon
#define dataRate 115200    // 9600kbps

int tagIndex = 0;                 // counter for number of bytes read
int allComplete = false;          // whether all tags have been read

char xVal[tagLength], yVal[tagLength],dVal[tagLength];

int xComplete = false;
int yComplete = false;
int dComplete = false;

int receiveData = false;

//////////////////////////////////////////


Servo penServo;

void setup()
{
  Serial.begin(115200);
 
  penServo.attach(6);
  penUp();
 
  pinMode(stepper1dir, OUTPUT);
  pinMode(stepper1step, OUTPUT);
  pinMode(stepper2dir, OUTPUT);
  pinMode(stepper2step, OUTPUT);

  Serial.print('S');
  Serial.println(); 

}

void loop()
{
 
    if (Serial.available() > 0) {
      delay(10);
      readData();   
    }
}


void calculateCommand(float x1,float y1,int d){
  x = abs(x1) - abs(prevX);
  y = abs(y1) - abs(prevY);
  prevX = x1;
  prevY = y1;
  stepsX = round(x/stepSize);
  stepsY = round(y/stepSize);   
 
  LCM = lcm(abs(stepsX),abs(stepsY));

  if(stepsX == 0){
    moduloX = 0;
  }
  else {
    moduloX = int(LCM/long(abs(stepsX)));
  }
  if(stepsY == 0){
    moduloY = 0;
   }
  else{
     moduloY = int(LCM/long(abs(stepsY)));
   }
   
  Serial.print(LCM);Serial.print(" ");Serial.print(moduloX);Serial.print(" ");Serial.println(moduloY);
 
  if(d==0){
    penUp();
    Speed = maxSpeed;
  }   
  else if(d==1){
    penDown();
    Speed = nSpeed;
  }
 
  if(stepsX > 0){
    motorsXpositive();
  }
  else {
    motorsXnegative();     
  }
 
  if(stepsY > 0){
    motorsYpositive();
  }
  else {
    motorsYnegative();     
  }       

  for(long i = 0; i<LCM; i++){
    mX++;
    mY++;
    if(mX == moduloX){
      stepMotorX();
      mX = 0;
    }
    if(mY == moduloY){
      stepMotorY();
      mY = 0;
    }
  }
   
  finish();
 
  Serial.print(" Steps x: ");Serial.print(stepsX);
  Serial.print(" Steps y: ");Serial.println(stepsY);
   
}


void finish(){
  Serial.println("S");
}


void stepMotorX(){
      digitalWrite(stepper1step, LOW);
      digitalWrite(stepper1step, HIGH);
      delayMicroseconds(Speed);
}

void stepMotorY(){
      digitalWrite(stepper2step, LOW);
      digitalWrite(stepper2step, HIGH);
      delayMicroseconds(Speed);
}


void motorsXpositive(){
    digitalWrite(stepper1dir, HIGH);     // Set the direction.
}
void motorsYpositive(){
    digitalWrite(stepper2dir, LOW);     // Set the direction.
}
void motorsXnegative(){
    digitalWrite(stepper1dir, LOW);     // Set the direction.
}
void motorsYnegative(){
    digitalWrite(stepper2dir, HIGH);     // Set the direction.
}


void penUp(){
  penServo.write(95);
}

void penDown(){
  penServo.write(105);
}


void readData() {
 char thisChar = Serial.read();
 
 if (thisChar == hash){
  // read all x
  while (xComplete == false && Serial.available() > 0){
      char thisChar = Serial.read();

      if (thisChar == semicolon){
        xComplete = true;
        break;
      }
      else {
        xVal[tagIndex] = thisChar;
        tagIndex++;
      } 
  }
 
  tagIndex = 0;
 
 // read all y
  while (yComplete == false && Serial.available() > 0){
      char thisChar = Serial.read();
 
      if (thisChar == semicolon){
        yComplete = true;
        break; 
    }
      else {
        yVal[tagIndex] = thisChar;
        tagIndex++;
      } 
  }
 
  tagIndex = 0;
 
  // read all d
  while (dComplete == false && Serial.available() > 0){
      char thisChar = Serial.read();
     
      if (thisChar == semicolon){
        dComplete = true;
        break;
      }
      else {
        dVal[tagIndex] = thisChar;
        tagIndex++;
      } 
  }

  tagIndex = 0;

 
  if (xComplete == true && yComplete == true && dComplete == true){
   
      calculateCommand(atof(xVal),atof(yVal),atof(dVal));
   
    xComplete = false;
    yComplete = false;
    dComplete = false;
   
    // empty arrays
    for (int i=0;i<5;i++){
    xVal[i] = 0x00;
    yVal[i] = 0x00;
    dVal[i] = 0x00;
    }
  }
 
 }
 
}




unsigned long gcd(long a, long b)
{
    if (b == 0)
      return a;
    else
      return gcd(b, a%b);
}


unsigned long lcm(long a, long b)
{
  if(a == b)
     return a;
  else if(a == 0)
     return b;
  else if(b == 0)
     return a;
  else
     return (a*b)/gcd(a,b);
}
The values that im sending is x y in mm.
3  Using Arduino / Programming Questions / Re: 2 Stepper motors arrive at same time on: May 25, 2014, 05:48:13 pm
Code:
      if((i % moduloX) == 0){
Aha, we've found the source of the slowness.

There are better ways to do this. You need to iterate through the for loop a certain number of times. On each iteration, increment a variable for the x direction and one for the y direction. When the x variable gets to the value needed to cause a step, step, and set the variable back to 0. Similarly for the y variable. Then, no modulo calls will be needed.

Ive tryed your suggestion but almost nothing changed, speed depends on the LCM result, if LCM is small the steppers will run faster, if its large the steppers run slower, which makes sense i guess.

I'm curious why you are not using the Stepper or AccelStepper library.
Ive tryed AccelStepper but your cant coordinate 2 stepper actions given different speeds.

If you need more speed why not do all the calculations OUTSIDE the loop that runs the motors?
...R
Can you give me an example because i didnt get it.

thank you
4  Using Arduino / Programming Questions / Re: 2 Stepper motors arrive at same time on: May 25, 2014, 09:22:47 am
Thanx  PaulS.
Here is my code for calculating the steps and give command to steppers
Code:
void calculateCommand(float x1,float y1,int d){
  x = abs(x1) - abs(prevX);
  y = abs(y1) - abs(prevY);
  prevX = x1;
  prevY = y1;
  stepsX = round(x/stepSize);
  stepsY = round(y/stepSize);   
 
  LCM = lcm(long(stepsX),long(stepsY));
  moduloX = int(LCM/long(stepsX));
  moduloY = int(LCM/long(stepsY));
 
  if(d==0){
    penUp();
    Speed = maxSpeed;
  }   
  else if(d==1){
    penDown();
    Speed = nSpeed;
  }
 

    for(long i = 0; i<LCM; i++){
      if((i % moduloX) == 0){
        if(stepsX > 0){
          motorsXpositive();
        }
        else {
          motorsXnegative();     
        }     
        stepMotorX();
      }
      if((i % moduloY) == 0){
        if(stepsY > 0){
          motorsYpositive();
        }
        else {
          motorsYnegative();     
        }       
        stepMotorY();
      }
    }
    finish();

   
}


unsigned long gcd(long a, long b)
{
    if (b == 0)
      return a;
    else
      return gcd(b, a%b);
}


unsigned long lcm(long a, long b)
{
  return (a*b)/gcd(a,b);
}

and here is how i give the steppers commands:
Code:
void stepMotorX(){
      digitalWrite(stepper1step, LOW);
      digitalWrite(stepper3step, LOW);
      digitalWrite(stepper1step, HIGH);
      digitalWrite(stepper3step, HIGH);
      delayMicroseconds(Speed);
}

void stepMotorY(){
      digitalWrite(stepper2step, LOW);
      digitalWrite(stepper4step, LOW);
      digitalWrite(stepper2step, HIGH);
      digitalWrite(stepper4step, HIGH);
      delayMicroseconds(Speed);
}
5  Using Arduino / Programming Questions / Re: 2 Stepper motors arrive at same time on: May 25, 2014, 08:52:43 am
Thanx for all the answers.
Yes the code is for a plotter.
Ive tryed the LCM method and it sheems to working prorperly but with a big problem:
Im using 1/8 microstepping so many times the LCM is very very large number (millions..) and its very slow
for example if i want to step motor1 with 2019steps and motor2 with 1058steps i have an 2136102 LCM, so i have to make a for loop with 2136102 loops.

The same thing will happend with Robin2 solution..
So now im stuck again..
6  Using Arduino / Programming Questions / 2 Stepper motors arrive at same time on: May 25, 2014, 05:51:01 am
Hello,
I have the following problem that im trying to solve:
I have 2 steppers motors that i want to move them simultaneously but with different speeds.
My goal is for example to move Stepper1 for 500steps and Stepper2 for 300steps and finish the movement at the exact same time.
An approach that i followed was with a modulo oparation so every x steps for Stepper1 move Stepper2 a step, but ofcourse its only working when the result of the division of steps1 and steps2 is even number.

Any help about how i can make it work would be appreciated

7  Using Arduino / Microcontrollers / Re: attiny85 interrupt rising edge on: August 29, 2013, 02:27:30 pm
thanx again jack.
i used the code you mentioned and its working as expected, but this detects the low level.
 
Code:
MCUCR &= ~(_BV(ISC01) | _BV(ISC00));      //INT0 on low level

according to the datasheet setting ISC01 and ISC00 to "1" it detects the rising edge.
im trying this change
Code:
MCUCR |= (_BV(ISC01) | _BV(ISC00));      //INT0 rising edge
but its not working.. any idea why?
8  Using Arduino / Microcontrollers / Re: attiny85 interrupt rising edge on: August 29, 2013, 01:49:58 pm
Oops i missed that. thank you
My question now is that can i get out of sleep with INT0?
here is my working code with state Change interrupt
Code:
#include <avr/sleep.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

int pinLed = 0;
int trimPin=3;

void setup(){
  pinMode(pinLed,OUTPUT);
  pinMode(1,INPUT);
  pinMode(3,INPUT);
 
  sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
  sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt
}

void loop(){

  digitalWrite(pinLed,HIGH);
  delay(2000);
  digitalWrite(pinLed,LOW);

 
  system_sleep();
}

void system_sleep() {
  cbi(ADCSRA,ADEN); // Switch Analog to Digital converter OFF
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
  sleep_mode(); // System sleeps here
  sbi(ADCSRA,ADEN);  // Switch Analog to Digital converter ON
}

ISR(PCINT0_vect) {
}
9  Using Arduino / Microcontrollers / attiny85 interrupt rising edge on: August 29, 2013, 12:47:15 pm
Hello,
Im programming a ATtiny85 with this library http://hlt.media.mit.edu/?p=1229 .
I want to create an interrupt on pin1 and im using this code:
Code:
  sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
  sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt
the interrupt works as expected, it fires an interrupt on state Change.
Now i only want the interrput to fire on rising edge and not on state change.
Can anybody help me?
thanx
10  Topics / Home Automation and Networked Objects / arduino RN-XV WiFly Module ad hoc on: March 26, 2013, 05:27:38 am
Hello,
Im trying to make a connection between my laptop and arduino using RN-XV WiFly Module.
I will describe what i want to achieve and what ive done so far.
Ive set the wifly in ad hoc mode,connected in the wifly from my laptop and using this code in Mega
Code:
void setup() {               
        Serial.begin(9600);
        Serial3.begin(9600);
}
void loop() {
     if (Serial3.available()) {
    Serial.write(Serial3.read());
}
}

and reaching the address that ive set in my browser (i.e 192.186.1.1/test ) im getting this GET
Code:
*CLOS**OPEN*GET /test HTTP/1.1
Host: 192.168.1.1
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
So everything ok so far, now i want to create an html file in my laptop(acting as a local webserver) in order to make a button to control a led and then get and print to the html some values from the arduino
Can anyone direct me in order to find how i can establish this communication?

thank you
11  Using Arduino / Microcontrollers / Re: Bootloader on custom board with atmega2560 on: October 23, 2012, 05:25:32 am
A small update:
I tryed to burn the bootloader with an arduino mega 2560 as ISP.
Ive connected a led in pin 7 and one in pin 8. Now im getting the following:
When i try to burn the bootloader the led in pin 7 is on.
The RX TX led on the ISP arduino mega are showing activity.
After a minute im getting this error
Code:
avrdude: verification error, first mismatch at byte 0x1e000
         0xff != 0x0d
avrdude: verification error; content mismatch
And after this, in my new board the led in pin13 blinks but not as expected, ON for a second and the OFF for a second,
its doing 2 fast blinks, then off and the same again all the time..
Im really confused!
Can anyone help me?
12  Using Arduino / Microcontrollers / Re: Bootloader on custom board with atmega2560 on: October 23, 2012, 02:17:53 am
I just realized that in my print screen i dont have all the components in my schematic, some of them was out of the screen and i didnt realized that.
Anyway ive attached the correct one.
As you can see i have an SPI header which is where i connect it to the arduino duemilanove loaded with the isp program.
13  Using Arduino / Microcontrollers / Bootloader on custom board with atmega2560 on: October 22, 2012, 12:33:04 pm
Hello,
Ive build a new board with an atMega2560 and now im trying to flash the arduino bootloader. The method is through  and Arduino Duemilanove as ISP.
Im pretty sure that the connections between the two boards are correct, but when im trying to burn the bootloader i get this error: "Invalid device signature" Ive also tried with this method http://www.gammon.com.au/forum/?id=11635 but still cant upload the bootloader.
Do i have any mistakes in my schematic or can anyone imagine something that is wrong?
The board was printed in a fab so the connections are just like in the schematic.
thank you
14  Topics / Home Automation and Networked Objects / arduino and Nano WiReach SMT on: March 05, 2012, 05:02:11 pm
Hello,
Im trying to build a web server with arduino and the Nano WiReach SMT http://www.connectone.com/products.asp?did=73&pid=103
So far i was able just to set up the Nano WiReach SMT to my wifi network,pack and load a HelloWorld html successfully.
Im trying to find any further info for this module in order to communicate with arduino.
Basicly i want to control some led's in the ardino and read an analog input..
Does anyone has any experience with this module?
15  Using Arduino / Programming Questions / Re: amarino, sending data from arduino to android on: August 31, 2011, 11:32:02 am
Here is what i found in the cpp file:
Code:
void MeetAndroid::send(char c ){
Serial.print(startFlag);
Serial.print(c);
Serial.print(ack);
}
   startFlag = 18;
   ack = 19;

and here is the link of amarino http://www.amarino-toolkit.net/
Pages: [1] 2 3