mirror of
https://github.com/NolanMascrier/arduino.git
synced 2025-10-30 15:26:00 +00:00
Support for 6 sensors at once
This commit is contained in:
parent
663192a1c9
commit
7d9285e6da
@ -12,11 +12,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Arduino_APDS9960.h>
|
#include <Arduino_APDS9960.h>
|
||||||
#include <ArduinoBLE.h>
|
//#include <WiFiNINA.h>
|
||||||
|
#include "arduino_secrets.h"
|
||||||
|
|
||||||
#define R 2
|
#define TRIGG_BOTTOM D12
|
||||||
#define O 3
|
#define TRIGG_TOP D10
|
||||||
#define V 4
|
#define TRIGG_1 D8
|
||||||
|
#define TRIGG_2 A0
|
||||||
|
#define TRIGG_3 A2
|
||||||
|
#define TRIGG_4 A4
|
||||||
|
|
||||||
|
#define ECHO_BOTTOM D11
|
||||||
|
#define ECHO_TOP D9
|
||||||
|
#define ECHO_1 D7
|
||||||
|
#define ECHO_2 A1
|
||||||
|
#define ECHO_3 A3
|
||||||
|
#define ECHO_4 A5
|
||||||
|
|
||||||
#define STATUS_SAFE 0
|
#define STATUS_SAFE 0
|
||||||
#define STATUS_CLOSE 1
|
#define STATUS_CLOSE 1
|
||||||
@ -26,9 +37,14 @@
|
|||||||
#define DISTANCE_SAFE 120
|
#define DISTANCE_SAFE 120
|
||||||
|
|
||||||
int status = STATUS_SAFE;
|
int status = STATUS_SAFE;
|
||||||
|
//int wifi_status = WL_IDLE_STATUS;
|
||||||
|
|
||||||
int ledState = LOW;
|
int ledState = LOW;
|
||||||
|
|
||||||
|
//please enter your sensitive data in the Secret tab
|
||||||
|
char ssid[] = SECRET_SSID; // your network SSID (name)
|
||||||
|
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
|
||||||
|
|
||||||
unsigned long previousMillis = 0;
|
unsigned long previousMillis = 0;
|
||||||
|
|
||||||
const long intervalLong = 750;
|
const long intervalLong = 750;
|
||||||
@ -40,85 +56,38 @@ const long intervalShort = 20;
|
|||||||
const int trigger = 12;
|
const int trigger = 12;
|
||||||
const int echo = 11;
|
const int echo = 11;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
long duration = 0;
|
long duration = 0;
|
||||||
int distance = 0;
|
int distance = 0;
|
||||||
|
|
||||||
BLEService ledService("180A");
|
long durations[6];
|
||||||
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
|
long distances[6];
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial)
|
Serial.println("Starting up");
|
||||||
;
|
|
||||||
|
|
||||||
if (!APDS.begin()) {
|
if (!APDS.begin()) {
|
||||||
Serial.println("Error initializing APDS-9960 sensor!");
|
Serial.println("Error initializing APDS-9960 sensor!");
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
if (!BLE.begin()) {
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
Serial.println("starting Bluetooth® Low Energy failed!");
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
BLE.setLocalName("Nano 33 BLE Sense");
|
//Init the sensors
|
||||||
BLE.setAdvertisedService(ledService);
|
pinMode(TRIGG_BOTTOM, OUTPUT);
|
||||||
ledService.addCharacteristic(switchCharacteristic);
|
pinMode(TRIGG_TOP, OUTPUT);
|
||||||
BLE.addService(ledService);
|
pinMode(TRIGG_1, OUTPUT);
|
||||||
switchCharacteristic.writeValue(0);
|
pinMode(TRIGG_2, OUTPUT);
|
||||||
BLE.advertise();
|
pinMode(TRIGG_3, OUTPUT);
|
||||||
|
pinMode(TRIGG_4, OUTPUT);
|
||||||
|
|
||||||
pinMode(trigger, OUTPUT);
|
pinMode(ECHO_BOTTOM, INPUT);
|
||||||
pinMode(echo, INPUT);
|
pinMode(ECHO_TOP, INPUT);
|
||||||
//LEDs
|
pinMode(ECHO_1, INPUT);
|
||||||
pinMode(R, OUTPUT);
|
pinMode(ECHO_2, INPUT);
|
||||||
pinMode(O, OUTPUT);
|
pinMode(ECHO_3, INPUT);
|
||||||
pinMode(V, OUTPUT);
|
pinMode(ECHO_4, INPUT);
|
||||||
analogWrite(R, 0);
|
|
||||||
analogWrite(O, 0);
|
|
||||||
analogWrite(V, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the status of processor according to the
|
|
||||||
* read distance.
|
|
||||||
*
|
|
||||||
* @arg distance : read distance from the sensor.
|
|
||||||
*/
|
|
||||||
void update_status(int distance)
|
|
||||||
{
|
|
||||||
if (distance <= DISTANCE_DANGER && status != STATUS_DANGER)
|
|
||||||
{
|
|
||||||
status = STATUS_DANGER;
|
|
||||||
Serial.print("DANGER ! Obstacle found at distance ");
|
|
||||||
Serial.print(distance);
|
|
||||||
Serial.println("cm");
|
|
||||||
|
|
||||||
analogWrite(R, 255);
|
|
||||||
analogWrite(O, 0);
|
|
||||||
analogWrite(V, 0);
|
|
||||||
}
|
|
||||||
else if (distance > DISTANCE_DANGER &&
|
|
||||||
distance <= DISTANCE_SAFE && status != STATUS_CLOSE)
|
|
||||||
{
|
|
||||||
status = STATUS_CLOSE;
|
|
||||||
Serial.print("WARNING ! Obstacle closing on at distance ");
|
|
||||||
Serial.print(distance);
|
|
||||||
Serial.println("cm");
|
|
||||||
|
|
||||||
analogWrite(R, 0);
|
|
||||||
analogWrite(O, 255);
|
|
||||||
analogWrite(V, 0);
|
|
||||||
}
|
|
||||||
else if (distance > DISTANCE_SAFE && status != STATUS_SAFE)
|
|
||||||
{
|
|
||||||
status = STATUS_SAFE;
|
|
||||||
Serial.println("All is good :)");
|
|
||||||
|
|
||||||
analogWrite(R, 0);
|
|
||||||
analogWrite(O, 0);
|
|
||||||
analogWrite(V, 255);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,9 +95,9 @@ void update_status(int distance)
|
|||||||
*
|
*
|
||||||
* @return calculated distance.
|
* @return calculated distance.
|
||||||
*/
|
*/
|
||||||
int read_sensor()
|
long fire_sensor(int trigger, int echo)
|
||||||
{
|
{
|
||||||
int dist;
|
long dist;
|
||||||
long dur;
|
long dur;
|
||||||
|
|
||||||
digitalWrite(trigger, LOW);
|
digitalWrite(trigger, LOW);
|
||||||
@ -136,24 +105,35 @@ int read_sensor()
|
|||||||
digitalWrite(trigger, HIGH);
|
digitalWrite(trigger, HIGH);
|
||||||
delayMicroseconds(10);
|
delayMicroseconds(10);
|
||||||
digitalWrite(trigger, LOW);
|
digitalWrite(trigger, LOW);
|
||||||
duration = pulseIn(echo, HIGH);
|
dur = pulseIn(echo, HIGH);
|
||||||
distance = duration * 0.034 / 2;
|
dist = dur * 0.034 / 2;
|
||||||
|
delay(50);
|
||||||
return (dist);
|
return (dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
BLEDevice central = BLE.central();
|
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
digitalWrite(trigger, LOW);
|
|
||||||
delayMicroseconds(2);
|
Serial.print("Measure number ");
|
||||||
digitalWrite(trigger, HIGH);
|
Serial.println(count);
|
||||||
delayMicroseconds(10);
|
distances[0] = fire_sensor(TRIGG_BOTTOM, ECHO_BOTTOM);
|
||||||
digitalWrite(trigger, LOW);
|
distances[1] = fire_sensor(TRIGG_TOP, ECHO_TOP);
|
||||||
duration = pulseIn(echo, HIGH);
|
distances[2] = fire_sensor(TRIGG_1, ECHO_1);
|
||||||
distance = duration * 0.034 / 2;
|
distances[3] = fire_sensor(TRIGG_2, ECHO_2);
|
||||||
update_status(distance);
|
distances[4] = fire_sensor(TRIGG_3, ECHO_3);
|
||||||
|
distances[5] = fire_sensor(TRIGG_4, ECHO_4);
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
Serial.print("Sensor {");
|
||||||
|
Serial.print(i);
|
||||||
|
Serial.print("} : ");
|
||||||
|
Serial.print(distances[i]);
|
||||||
|
Serial.println("cm");
|
||||||
|
}
|
||||||
|
//update_status(distance);
|
||||||
|
|
||||||
// wait a bit before reading again
|
// wait a bit before reading again
|
||||||
delay(350);
|
delayMicroseconds(1000000);
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user