Machsupport Forum

Third party software and hardware support forums. => Modbus => Topic started by: zafarsalam on November 20, 2012, 01:12:31 AM

Title: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on November 20, 2012, 01:12:31 AM
Here's a simple tutorial describing the steps for setting analog input to Mach3 through Arduino. FRO and Spindle override are shown in the example. Any of the dro's in Mach3 can be controlled in this way.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: dotn on January 19, 2013, 08:13:00 AM
Good job works perfect
I am new to this and would like to put buttons and LED on the arduino but I can not run or signals the brain
can you help

Thanks
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on January 20, 2013, 02:47:55 AM
Good job works perfect
I am new to this and would like to put buttons and LED on the arduino but I can not run or signals the brain
can you help

Thanks


For this you'll have to make changes in arduino sketch, modbus settings and brains in Mach3. I'll get back to it in a day or two with that.
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: aluplastvz on April 09, 2015, 04:08:34 PM
Hello Zafar
how to connect your analog input with this code
greeting


unsigned int CRC16, SEED, GP; //for CRC16
#define SEED 0xFFFF  //initialization for CRC16
#define GP   0xA001  //generating polynomial
int in_buffer[8]; // receiving buffer

#define CycleStart 2;  //MOD:0-D0
#define FeedHold 3;  //MOD:0-D1
#define StopFile 4;  //MOD:0-D2
#define SingleStep 5;  //MOD:0-D3
#define ResumeFile 6;  //MOD:0-D4
#define ZeroX 7;  //MOD:0-D5
#define ZeroY 8;  //MOD:0-D6
#define ZeroZ 9;  //MOD:0-D7


void setup()
{
  Serial.begin(9600);            // Init serial communication
  pinMode(2, INPUT);           // Set pin to input
  pinMode(3, INPUT);           // Set pin to input
  pinMode(4, INPUT);           // Set pin to input
  pinMode(5, INPUT);           // Set pin to input
  pinMode(6, INPUT);           // Set pin to input
  pinMode(7, INPUT);           // Set pin to input
  pinMode(8, INPUT);           // Set pin to input
  pinMode(9, INPUT);           // Set pin to input
  digitalWrite(2, HIGH);       // Turn on pullup resistor
  digitalWrite(3, HIGH);       // Turn on pullup resistor
  digitalWrite(4, HIGH);       // Turn on pullup resistor
  digitalWrite(5, HIGH);       // Turn on pullup resistor
  digitalWrite(6, HIGH);       // Turn on pullup resistor
  digitalWrite(7, HIGH);       // Turn on pullup resistor
  digitalWrite(8, HIGH);       // Turn on pullup resistor
  digitalWrite(9, HIGH);       // Turn on pullup resistor
}

void loop()
{
   if ( Serial.available() == 8 ) {
     
     // read ModBus command from Mach3
     for (int i=0; i < 8; i++) in_buffer[ i ] = Serial.read();
     
     if ((in_buffer[0]== 0x01)&(in_buffer[1]== 0x04))
         Reply_Inputs();
         else
         Reply_Error();
         
     Serial.flush();
 
   }   
}

/// ----------------------- Functions -----------------------
void Reply_Inputs()
{
   CRC16 = SEED;
   int Inputs[] ={0x01, 0x04, 0x0A, 0x00, 0x00,
                  0x00, 0x00, 0x00, 0x00, 0x00,
                  0x00, 0x00, 0x00, 0x00, 0x00};

   Inputs[4] = (
               ((!digitalRead(9))<<7) |
               ((!digitalRead(8))<<6) |
               ((!digitalRead(7))<<5) |
               ((!digitalRead(6))<<4) |
               ((!digitalRead(5))<<3) |
               ((!digitalRead(4))<<2) |
               ((!digitalRead(3))<<1) |
               ((!digitalRead(2))   ) );

   for (int i = 0; i < 13; i++)
        {   
        Calc_CRC(Inputs, &CRC16);
        }     
       
        Inputs[13]=CRC16;
        CRC16=CRC16>>8;
        Inputs[14]=CRC16;
   
   for (int i=0; i < 15; i++) Serial.write(Inputs);
}
/// ---------------------------------------------------------
void Reply_Error()
{
   int Error[] ={0x01, 0x84, 0x00, 0x43, 0x00};
   for (int i=0; i < 5; i++) Serial.write(Error);
}
/// ---------------------------------------------------------
void Calc_CRC(unsigned char b, unsigned int* CRC)
{
   int carry, i;

   CRC[0] ^= b & 0xFF;
   for (i=0; i<8; i++)
   {
      carry = CRC[0] & 0x0001;
      CRC[0]>>=1;
      if (carry) CRC[0] ^= GP;
   }
}
/// ---------------------------------------------------------
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on April 10, 2015, 07:24:40 AM
I presume you want to use external buttons to control Mach3 through Arduino. For this you'll need to use Mach's brain too. I'll work out an Arduino sketch and the brain and post it here.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: aluplastvz on April 10, 2015, 07:51:01 AM
Do I want to have foreign keys and 2 analog like you did in your case
thank you
greeting
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on April 10, 2015, 11:41:53 AM
You'll need foreign keys to short the pins 2,3,4....9 with the ground on arduino. No need to use the external pots if you don't need the fro and sso functions.
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on April 10, 2015, 12:05:05 PM
You'll have to make additions to the Arduino sketch like this

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
}

void loop()
(
  regs[0]=!digitalRead(2);
  regs[1]=!digitalRead(3);
  regs[2]=!digitalRead(4);
  regs[3]=!digitalRead(5);
  regs[4]=!digitalRead(6);
  regs[5]=!digitalRead(7);
  regs[6]=!digitalRead(8);
  regs[7]=!digitalRead(9);
}
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: aluplastvz on April 10, 2015, 12:31:07 PM
Regards To Far
I'm sad I did not understand
I want 2 analog inputs for potenciometa like you do and I make the 10 input.
Arduino cod 10 input functions
You with 2 analog inputs function. how to put them into one program Arduino cod
Brain is not a problem
Sorry for my bad english
greeting
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: aluplastvz on April 13, 2015, 12:02:29 PM
I managed to put together two programs diginput 8 + 2analog
x + .x-.y + .y-.z + .z-.start, stop spindle% + -, feed rate + -

Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on April 13, 2015, 11:21:29 PM
That's really great. I am out of country these days, so couldn't reply to your posts in time. Looks like you have figured it out yourself.

Or did you want these 8 switches on a single pot. This is possible too. Just use the map() and switch case functions in Arduino. See the command reference and examples on Arduino website.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: linuxfan on November 30, 2015, 12:05:37 PM
Thank you for this really great piece of arduino code! Reading arduino pins, and sending data to mach3 is awsome feature. Can you please point me on how to send data from mach3 to arduino?
I would like to send for example X position DRO value to arduino, and then print out data on display. Can I just define variable as double, and ecquire its value like this in loop:

my_variable=regs[10];

And then in mach3 brains select data input as X DRO, and then terminate as modbus address 10, and select it as Output?
Or does this code work only one way, from arduino to mach3, and some major code correction has to be done to work other way?

Thank you,
Simon
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: khalid on November 25, 2016, 08:51:34 AM
Zafar Bhai..u still out there?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on November 30, 2016, 06:49:07 PM
Zafar Bhai..u still out there?

Yes right here in Malaysia. Will be back this Sunday.
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: nokialkatel on January 14, 2017, 06:11:52 PM
hi! I need help from you zafarsalam

How to setup dro info on lcd connected to arduino modbus slave on arduino with shillings code modbus slave???

Ihave included wire.h and liquidcristali2c.h for lcd in arduino code.
From now I ned help for how to send data from mach3 -->>>brain-->>serial modbus plugin--->>>arduino modbus slave ?????
many users have this done but no documentation on arduino code, no info about serial plugin configs,

Help  and thanks
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on January 14, 2017, 09:07:43 PM
hi! I need help from you zafarsalam

How to setup dro info on lcd connected to arduino modbus slave on arduino with shillings code modbus slave???

Ihave included wire.h and liquidcristali2c.h for lcd in arduino code.
From now I ned help for how to send data from mach3 -->>>brain-->>serial modbus plugin--->>>arduino modbus slave ?????
many users have this done but no documentation on arduino code, no info about serial plugin configs,

Help  and thanks

I am out of country right now. Will give you some tips on it when back home by this weekend.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: nokialkatel on January 19, 2017, 04:24:18 PM
Are you home Zafarsallam?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on January 27, 2017, 04:03:55 AM
Yes. I am here. I haven't tried shillings code on modbus. I am using Vinny's code. You can download it from the link on this thread. I have used it to display Mach DROs on LCD screen with arduino. I'll fish out the code, tidy it up and share it here in a few days.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: nokialkatel on January 27, 2017, 04:45:59 AM
Ok.thanks! I am waiting.
Can not find villy's code.
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on February 03, 2017, 10:24:20 PM
Ok.thanks! I am waiting.
Can not find villy's code.

Download the pdf file in the first post on this thread. The links are in there.
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: lixiaobing on April 03, 2017, 09:28:44 AM
 ???[/img]Hello, how can I add jog c ++? I have used arduino, mach3, modbus. In the current mach3 I have added jogx ++, jogx, jog y ++, jogy, jogz ++, jogz -, joga ++, joga -,What is your email? I am 1040373253@qq.com
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on April 04, 2017, 09:41:23 AM
???[/img]Hello, how can I add jog c ++? I have used arduino, mach3, modbus. In the current mach3 I have added jogx ++, jogx, jog y ++, jogy, jogz ++, jogz -, joga ++, joga -,What is your email? I am 1040373253@qq.com

Use ButtonPress as the terminal lobe. Scan code 331 and 332 are for Jogging C axis.

Zafar
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: lixiaobing on April 04, 2017, 10:40:12 PM
???[/img]Hello, how can I add jog c ++? I have used arduino, mach3, modbus. In the current mach3 I have added jogx ++, jogx, jog y ++, jogy, jogz ++, jogz -, joga ++, joga -,What is your email? I am 1040373253@qq.com

Use ButtonPress as the terminal lobe. Scan code 331 and 332 are for Jogging C axis.

Zafar
Thanks, I got it out. But I used the picture, the effect is very good
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: Jan Mozol on June 07, 2017, 01:46:03 PM
Hello Zafar..i'm a newbie on this, where can I get Vinys code? and should I just upload it to a clean Uno?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: zafarsalam on June 07, 2017, 11:54:49 PM
Download links are right there in the tutorial pdf.

Hello Zafar..i'm a newbie on this, where can I get Vinys code? and should I just upload it to a clean Uno?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: Jan Mozol on June 08, 2017, 08:31:24 AM
Thanks , could you give me direcions if is it possible to use a digital trigger in modbus?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: brianhatley on November 07, 2021, 08:00:27 PM
I Know this thread is old, but its exactly the information looking for - i just need some help on the subject - hopefully yall are still around.  I did this mod, and it works great, only issue is the values are bouncing around very erratically. is there a fix for this? is it normal? will this cause issues in my programs with so many abrupt changes to feed rate and spindle speed?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: TheSuperior on March 25, 2022, 02:33:42 AM
Hi there,

I see this has been posted ages ago.

I want to do something similar with a ATC. It should send an integer value between 1 to 12 to the arduino every time it encounters the M6 Gcode

ex. M6 T0101 - should send "1" to the arduino.

Any idea how to do this?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: Giuno85 on October 06, 2022, 11:59:24 AM
hi zafarsalam, i am a new user too, i found your work on the application of potentiometers very useful and beautiful.
I tried to implement the code to add control buttons but failed.
I started from your sketch I implemented the entries but there is no way to make the buttons work.
Could you kindly explain to me and tell us where to go to edit the sketch?
Title: Re: Simple tutorial on analog inputs with Arduino
Post by: Giuno85 on October 10, 2022, 03:40:24 PM
I solved