I have a project (using an Arduino UNO) that involves reading data from 2 sensors (using ADS1115 ADC) and start/stop control for a stepper motor (controlled via Pulse and Direction pins) but whenever the function printvoltstopc() runs it slows down the motor so much to the point where the motor is not useful, and to troubleshoot the problem I commented out printvoltstopc() and then the motor runs fine so I think the problem here is that printvoltstopc() is too slow any suggestions on how I can make this work, here is my code:
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
const int pulsePin = 9;
const int dirPin = 8;
bool startCollecting = false;
int motorSpeed = 0;
Adafruit_ADS1115 _ADC;
void motorPulse(int speed)
{
digitalWrite(pulsePin, HIGH);
delayMicroseconds(speed);
digitalWrite(pulsePin, LOW);
delayMicroseconds(50);
}
void printVoltsToPc()
{
int16_t adc0 = _ADC.readADC_SingleEnded(0); // Read from A0
int16_t adc2 = _ADC.readADC_SingleEnded(2); // Read from A2
float volts0 = _ADC.computeVolts(adc0);
float volts2 = _ADC.computeVolts(adc2);
Serial.print(volts0);
Serial.print(",");
Serial.println(volts2);
}
void recieveCommand()
{
if (Serial.available())
{
String command = Serial.readStringUntil('\0');
if (command.startsWith("R"))
{
motorSpeed = command.substring(2).toInt();
digitalWrite(dirPin, HIGH);
startCollecting = true;
}
else if (command.startsWith("L"))
{
motorSpeed = command.substring(2).toInt();
digitalWrite(dirPin, LOW);
startCollecting = true;
}
else if (command == "S")
{
startCollecting = false;
digitalWrite(pulsePin, LOW);
}
}
}
void setup()
{
pinMode(pulsePin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
_ADC.begin();
}
void loop()
{
recieveCommand();
if (startCollecting)
{
printVoltsToPc();
motorPulse(motorSpeed);
}
}
