I have an arduino uno connected to a stepper motor via a driver and a max30100 purple edition (functioning model). When I have the HR sensor code by itself everything works, but when I added some other code from my stepper it stopped sending data. Any suggestions, its for a school project due on Friday 9th sep.
(Working by itself)
(Stops working with stepper code added (code below)) (no data sent always stays at zero)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
PulseOximeter pox;
uint32_t tsLastReport = 0;
#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int stepArray[5] = {0, 60, 90, 120, 120};
int selectStep;
int stepSelect = 1;
int timeArray[5] = {0, 2, 3, 4, 4};
int selectTime;
int speedSelect = 1;
const byte limitSwitch = 4;
const byte buttonPin = 6;
void setup() {
Serial.begin(9600) ;
lcd.init();
lcd.backlight();
stepper.setMaxSpeed(1000);
if (!pox.begin()) {
Serial.println("FAILED");
for (;;);
} else {
Serial.println("SUCCESS");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
while (digitalRead(buttonPin) != HIGH) {}
PinMode(4, INPUT);
PinMode(6, INPUT);
Serial.println(digitalRead(limitSwitch));
Serial.println(digitalRead(buttonPin));
delay(1000);
if (digitalRead(limitSwitch) == 1) {
// Do Nothing
}
else if (digitalRead(limitSwitch) == 0) {
long arbitraryStep = 50000;
stepper.setCurrentPosition(0);
delay(1000);
while ((stepper.currentPosition() != arbitraryStep) && (digitalRead(limitSwitch) == 0)) {
stepper.setSpeed(1);
stepper.runSpeed();
delay(1000);
Serial.print(digitalRead(limitSwitch));
}
}
}
void loop() {
stepSelect = map(analogRead(A1), 0, 1024, 1, 5);
selectStep = stepArray[stepSelect];
lcd.setCursor(0, 0);
lcd.print("TIDALVOLUME:");
lcd.setCursor(12, 0);
lcd.print((selectStep / 30) - 1);
lcd.setCursor(0, 1);
speedSelect = map(analogRead(A0), 0, 1024, 1, 5);
selectTime = timeArray[speedSelect];
lcd.print("BREATHSPERMINUTE:");
lcd.print(60 / (selectTime + 2));
unsigned long cTime = millis();
while ((millis() - cTime) < 500) {// wait for 500 milliseconds
pox.update();
}
stepper.setCurrentPosition(0);
while (stepper.currentPosition() != selectStep)
{
pox.update();
stepper.setSpeed(int(selectStep / (selectTime)));
stepper.runSpeed();
}
cTime = millis();
while ((millis() - cTime) < 500) {// wait for 500 milliseconds
pox.update();
}
// Reset the position to 0:
stepper.setCurrentPosition(0);
cTime = millis();
while ((millis() - cTime) < 500) {// wait for 500 milliseconds
pox.update();
}
// Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
while (stepper.currentPosition() != -selectStep)
{
pox.update();
stepper.setSpeed(-int(selectStep / (selectTime)));
stepper.runSpeed();
}
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
// lcd.clear(); // Clear LCD
lcd.setCursor(0, 2);
lcd.print("HR:");
lcd.setCursor(3, 2);
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
lcd.print(pox.getHeartRate());
lcd.setCursor(0, 3);
lcd.print("SPO2:");
lcd.setCursor(5, 3);
float spo2Value = pox.getSpO2();
lcd.print(spo2Value);
Serial.print("bpm / SpO2:");
Serial.print(spo2Value);
Serial.println("%");
if (spo2Value < 90) {
// play a note on pin 6 for 1 second :
tone(5, 440, 1000);
}
cTime = millis();
while ((millis() - cTime) < 500) {// wait for 500 milliseconds
pox.update();
}
tsLastReport = millis();
}
cTime = millis();
while ((millis() - cTime) < 500) {// wait for 500 milliseconds
pox. Update();
}
}type or paste code here