Hello, I am working on a school project with a friend. We are trying to have the Arduino Uno send encoder values from a stepper motor through serial to a computer running ROS 2 nodes. We also want the Uno to receive values from a ROS 2 node through the same serial connect. Messages are able to be sent back and forth using serial at the cost of the stepper motor rotating. The code provided spins the motor depending on the amount of microseconds of delay we provide between pulses to the driver to the motor. Any additional code added to the code below causes the motor to not spin anymore. I left a comment in the code on the types of functions that cause the motor to not rotate. We are kind of at a dead end right now and don't know where the issue is coming from. Any helpful criticisms and potential solutions are welcome. Thank you!
(Note: I know we could use functions to set digital pins as outputs and delayMicroseconds() to delay but we were tasked to use the registers to achieve the same results).
unsigned int start;
int microSeconds = 50;
unsigned int cycles;
void setup() {
// Initialize serial communication for printing readings
Serial.begin(9600);
TCCR0A = 0;
TCCR0B = 1;
// Set pin modes for motor control
// set pins 6 and 7 to output
DDRD = B11000000;
PORTD = B00000000;
}
void loop() {
//These types of commands and/or general calculations make the wheel stop spinning.
/*Serial.write(360);
readValue = Serial.readString();*/
// Calculate the number of clock cycles for a 50-microsecond delay
cycles = microSeconds * (F_CPU / 4000000UL);
// Start the delay
start = TCNT0;
//set pins 6 and 7 high for 50 microseconds
PORTD = B11000000;
// Wait for the specified number of cycles
while ((unsigned int)(TCNT0 - start) < cycles) {}
// Start the delay
start = TCNT0;
//set pin 7 low for 50 microseconds
PORTD = B01000000;
// Wait for the specified number of cycles
while ((unsigned int)(TCNT0 - start) < cycles) {}
}