Created
January 2, 2025 15:27
zirafa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TimerOne.h> | |
const int encoderPinA = 2; // Connected to interrupt 0 (pin 2) | |
const int encoderPinB = 3; // Connected to interrupt 1 (pin 3) | |
const int stepPin = 9; | |
const int dirPin = 8; | |
// Variables to track encoder state | |
volatile int32_t encoderPositionX = 0; | |
volatile int32_t encoderPositionY = 0; | |
volatile int32_t encoderPositionZ = 0; | |
volatile bool lastEncodedState = false; | |
volatile int32_t currentStateX = 0; // Variable to track current state of generated pulses | |
volatile int32_t currentStateY = 0; // Variable to track current state of generated pulses | |
volatile int32_t currentStateZ = 0; // Variable to track current state of generated pulses | |
int32_t step = 10; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(encoderPinA, INPUT_PULLUP); | |
pinMode(encoderPinB, INPUT_PULLUP); | |
pinMode(A0, INPUT_PULLUP); | |
pinMode(A1, INPUT_PULLUP); | |
pinMode(A2, INPUT_PULLUP); | |
pinMode(A3, INPUT_PULLUP); | |
pinMode(A4, INPUT_PULLUP); | |
pinMode(A5, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(encoderPinA), handleEncoder, CHANGE); | |
attachInterrupt(digitalPinToInterrupt(encoderPinB), handleEncoder, CHANGE); | |
Timer1.initialize(10000); | |
Timer1.attachInterrupt(pulsGenerator); | |
// Timer1.setPeriod(unsigned long microseconds) | |
} | |
bool x1, xx1, x10, xx10, x100, xx100; | |
volatile bool X, Y, Z ; | |
void loop() { | |
x1 = digitalRead(A0); | |
x10 = digitalRead(A1); | |
x100 = digitalRead(A2); | |
X = digitalRead(A3); | |
Z = digitalRead(A4); | |
Y = digitalRead(A5); | |
if (x1 != xx1) { | |
if (x1 == 0) Timer1.setPeriod(10000); | |
xx1 = x1; | |
} | |
if (x10 != xx10) { | |
if (x10 == 0) Timer1.setPeriod(1000); | |
xx10 = x10; | |
} | |
if (x100 != xx100) { | |
if (x100 == 0) Timer1.setPeriod(100); | |
xx100 = x100; | |
} | |
Serial.print(" X: "); | |
Serial.print(encoderPositionX); | |
Serial.print(" Y: "); | |
Serial.print(encoderPositionY); | |
Serial.print(" Z: "); | |
Serial.print(encoderPositionZ); | |
Serial.print(" A0 X1: "); | |
Serial.print(digitalRead(A0)); | |
Serial.print(" A1 X10: "); | |
Serial.print(digitalRead(A1)); | |
Serial.print(" A2 X100: "); | |
Serial.print(digitalRead(A2)); | |
Serial.print(" A3 X: "); | |
Serial.print(digitalRead(A3)); | |
Serial.print(" A5 Y: "); | |
Serial.print(digitalRead(A5)); | |
Serial.print(" A4 Z: "); | |
Serial.println(digitalRead(A4)); | |
delay(1000); | |
} | |
void handleEncoder() { | |
// Read the current state of the encoder pins | |
bool currentStateA = digitalRead(encoderPinA); | |
bool currentStateB = digitalRead(encoderPinB); | |
// Determine the direction based on the state transitions | |
if (currentStateA != lastEncodedState) { | |
if (currentStateA == currentStateB) { | |
if(X == 0) encoderPositionX += step; | |
if(Z == 0) encoderPositionZ += step; | |
if(Y == 0) encoderPositionY += step; | |
} else { | |
if(X == 0 ) encoderPositionX -= step; | |
if(Z == 0 ) encoderPositionZ -= step; | |
if(Y == 0 ) encoderPositionY -= step; | |
} | |
} | |
// Update the last state | |
lastEncodedState = currentStateA; | |
} | |
void controlStepperMotor(bool direction) { | |
// Set the stepper motor direction | |
digitalWrite(dirPin, direction); | |
// Generate a pulse to move the stepper motor | |
digitalWrite(stepPin, HIGH); | |
delayMicroseconds(1); // Pulse width, adjust as needed | |
digitalWrite(stepPin, LOW); | |
delayMicroseconds(1); // Time between pulses, adjust as needed | |
} | |
void pulsGenerator() { | |
if (currentStateX < encoderPositionX) { | |
currentStateX++; | |
} | |
if (currentStateX > encoderPositionX) { | |
currentStateX--; | |
} | |
if (currentStateY < encoderPositionY) { | |
currentStateY++; | |
} | |
if (currentStateY > encoderPositionY) { | |
currentStateY--; | |
} | |
if (currentStateZ < encoderPositionZ) { | |
currentStateZ++; | |
} | |
if (currentStateZ > encoderPositionZ) { | |
currentStateY--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment