Hi!
I'm trying to get arduino read pulses coming from my cnc machine steppers, which are driven by usb motion board.
Interrupts are counting all pulses when I'm not zeroing pulse counters (x and y), but when in the code there is line x=0; and y=0; then interrupts are not working. it only counts about 4 pulses instead of 2000..
here is code ask more if needed. Arduino board is uno copy from china. I had this working at somepoint. not sure why it's not working anymore..
When I uncomment those lines at middle of code the interrupts no more count all but those 4 pulses or less..
const byte IX =2;
const byte IY =3;
byte ntila = 0;
int rpm = 12000;
byte laser =80;
int nvar = 1000;
//näyttö sketsi
byte PWM_out = 6; //Pwm out
volatile int x = 0; // X-ax pulses
volatile int y = 0; // Y-ax pulses
float PWM = 0; // PWM strength
int ON = 10;
int var= 0;
float PWM_nop = 0;
byte koe =0;
//laser sketsi
#include <Key.h>
#include <Keypad.h>
const byte ROWS = 2; //four rows
const byte COLS = 2; //three columns
byte keys[ROWS][COLS] = {
{'1','3'},
{'2','4'},
};
byte rowPins[ROWS] = {5, 4,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
lcd.begin(16,2);
ntila=2;
lcd.home ();
pinMode(11, OUTPUT);
pinMode(IX, INPUT_PULLUP);
pinMode(IY, INPUT_PULLUP);
pinMode(ON, INPUT_PULLUP);
pinMode(PWM_out, OUTPUT); // pwm ulostulo pinni 6
attachInterrupt(digitalPinToInterrupt(IX), XA, FALLING); // X akseli pinni 2 kun menee high->low
attachInterrupt(digitalPinToInterrupt(IY), YA, FALLING); // Y akseli pinni 3
Serial.begin(9600); //
}
//
void loop() {
PWM = sqrt(sq((x))+sq(y));
//Serial.println(PWM*6.1, DEC); // matka kuljettu
Serial.print("x = ");
Serial.print(x);
Serial.print(" y = ");
Serial.println(y);
delay(100); //updates pwm signal
//koe = digitalRead(ON);
//if (koe == LOW && ntila==1) {
// Serial.print("toimi1 jyrsin");
// digitalWrite ( 11, LOW);
// x = 0;
// y = 0;
// analogWrite(PWM_out, PWM*0);
//
// }
//
//else if (koe==LOW && ntila==2) {
// Serial.print("toimi1 laser");
// digitalWrite ( 11,HIGH);
// analogWrite(PWM_out, (PWM/PWM_nop));
// x = 0;
// y = 0;
//}
//
//else {
// analogWrite(PWM_out, PWM*0);
// digitalWrite ( 11,HIGH);
// x=0;
// y=0;
// }
byte key = keypad.getKey();
if (key == 49 && rpm<11900 ){
rpm=rpm+nvar;
ntila=1;
Serial.println(key);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Jyrsinta RPM");
lcd.setCursor(9,1);
lcd.print(rpm);
}
if (key == 50 && laser<100 ){
laser++;
ntila=2;
Serial.println(key);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Laser Teho");
lcd.setCursor(9,1);
lcd.print(laser);
}
if (key == 51 && rpm>0 ){
rpm=rpm-nvar;
ntila=1;
Serial.println(key);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Jyrsinta RPM");
lcd.setCursor(9,1);
lcd.print(rpm);
}
if (key == 52 && laser>0 ){
laser--;
ntila=2;
Serial.println(key);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Laser Teho");
lcd.setCursor(9,1);
lcd.print(laser);
}
}
// Interrupti rutiinit
void YA() {
y++; }
void XA() {
x++; }