Engine + Sensors

Hello,
In my project, I want to get on the serial monitor of the arduino IDE the data of two sensors (BMP280 and Sharp IR ) and in addition to make a servomotor turn.
The problem is that the motor turns correctly but I receive the data only once and not every second. I don't see where the error is in my program.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <Servo.h>

#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)

Servo myservo;
int pos = 0;

int Capteur_distance = A0;

Adafruit_BMP280 bmp; // I2C

void setup() {
myservo.attach(9);

Serial.begin(9600);
Serial.println(F("BMP280 test"));

//if (!bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID)) {
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
while (1) delay(10);
}

bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X2,
Adafruit_BMP280::SAMPLING_X16,
Adafruit_BMP280::FILTER_X16,
Adafruit_BMP280::STANDBY_MS_500);
}

void loop() {

float volts = analogRead(Capteur_distance) * 0.0048828125;
int distance = 5 * pow (volts, -1);

Serial.print (distance);
Serial.print(",");

Serial.print(bmp.readTemperature());
Serial.print(",");

Serial.print(bmp.readPressure());
Serial.print(",");

Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println();

delay(1000);

for (pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(200);
}

for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(200);
}
}

Thanks for your advice.

That for loop (and the other one) block execution for 18 seconds, each. So, yeah, getting data every second will be problematic.

Maybe investigate millis() non-blocking timing.
Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.