Hi Everyone,
I have a project that is near done, I can find a solution by using two Arduino's but I'd rather have everything running off of one Arduino.
My aim is to constantly read the ADS1115, while simultaneously running the motor back and forwards.
-
I am driving a stepper motor 360 CW & 360 CCW in a loop, this works
-
I am reading values from I2C from a ADS1115 ADC breakout, this also works
However I tried to combine my codes using millis() but the stepper motor cycle would have to complete before reading the sensor once and SerialPrinting to the monitor (Where the sensor data needs to print). Then returning to 360 CW / 360 CCW.
Is there a way to do this without using two arduinos?
Thanks for your time
#include <Wire.h>
#include <DFRobot_ADS1115.h>
#include <SPI.h>
#include <HighPowerStepperDriver.h>
DFRobot_ADS1115 ads(&Wire);
HighPowerStepperDriver sd;
//declare millis operation
const unsigned long eventTime_stepper = 500;
const unsigned long eventTime_ADC_Check = 3; //16-bit ADC sample rate 860 per/sec or 1.16ms/sample, every 5 should be fine, set to 3 so it won't conflict with stepper time
unsigned long previousTime_Stepper = 0;
unsigned long previousTime_ADC = 0;
//declare stepper pins
const uint8_t DirPin = 2;
const uint8_t StepPin = 3;
const uint8_t CSPin = 4;
const uint16_t StepPeriodUs = 2000;
//declare data types for calculations
float torque;
//Control motor from C#
String Q;
String readString;
//Assign pins to LED
int red_light_pin = 11;
int green_light_pin = 10;
int blue_light_pin = 9;
void setup(void)
{
//ADC Setup
Serial.begin(115200);
ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS0); // 0x48
ads.setGain(eGAIN_TWOTHIRDS); // 2/3x gain
ads.setMode(eMODE_SINGLE); // single-shot mode
ads.setRate(eRATE_128); // 128SPS (default)
ads.setOSMode(eOSMODE_SINGLE); // Set to start a single-conversion
ads.init();
//Stepper motor Setup
SPI.begin();
sd.setChipSelectPin(CSPin);
pinMode(StepPin, OUTPUT);
digitalWrite(StepPin, LOW);
pinMode(DirPin, OUTPUT);
digitalWrite(DirPin, LOW);
delay(1);
sd.resetSettings();
sd.clearStatus();
sd.setDecayMode(HPSDDecayMode::AutoMixed);
sd.setCurrentMilliamps36v4(1000);
sd.setStepMode(1);
sd.enableDriver();
//RGB LED Output mode
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop(void) {
unsigned long currentTime = millis();
if (currentTime - previousTime_Stepper >= eventTime_stepper) {
doStepperFlip();
//Q = readString;
if (currentTime - previousTime_ADC >= eventTime_ADC_Check) {
if (ads.checkADS1115()) {
int16_t adc0;
int16_t tADC = 0;
int16_t torque = 0;
//Q = readString;
adc0 = ads.readVoltage(0); //this could be the + wiper pin from torque sensor
torque = (adc0*2)/1000;
Serial.println(torque); //our juicy data, likely I'll just need this print for C#. the surrounding prints are for EXCEL
previousTime_ADC = currentTime; //clock catch up
}
else
{
Serial.println("ADS1115 Disconnected!");
}
}
}
}
void step()
{
// The STEP minimum high pulse width is 1.9 microseconds.
digitalWrite(StepPin, HIGH);
delayMicroseconds(2);
digitalWrite(StepPin, LOW);
delayMicroseconds(2);
}
void setDirection(bool dir)
{
delayMicroseconds(1);
digitalWrite(DirPin, dir);
delayMicroseconds(1);
}
static void doStepperFlip () {
setDirection(0);
for (unsigned int x = 0; x < 2800; x++)
{
step();
delayMicroseconds(StepPeriodUs);
}
// Wait for 300 ms.
delay(300);
setDirection(1);
for (unsigned int x = 0; x < 2800; x++)
{
step();
delayMicroseconds(StepPeriodUs);
}
// Wait for 300 ms.
delay(300);
}