Skip to content
Snippets Groups Projects
Commit db3ad9dd authored by Rene Ebeling's avatar Rene Ebeling
Browse files

wrote a new arduino sketch

parent 49498008
No related branches found
No related tags found
No related merge requests found
...@@ -339,6 +339,109 @@ void processSensorData(VL53L7CX &sensor, VL53L7CX_ResultsData &results, const Js ...@@ -339,6 +339,109 @@ void processSensorData(VL53L7CX &sensor, VL53L7CX_ResultsData &results, const Js
for (int y = imageWidth * (imageWidth - 1); y >= 0; y -= imageWidth) { for (int y = imageWidth * (imageWidth - 1); y >= 0; y -= imageWidth) {
for (int x = 0; x <= imageWidth - 1; x++) { for (int x = 0; x <= imageWidth - 1; x++) {
sensorData.add(results.distance_mm[VL53L7CX_NB_TARGET_PER_ZONE * (x + y)]); sensorData.add(results.distance_mm[VL53L7CX_NB_TARGET_PER_ZONE * (x + y)]);
}
void I2C_RST(int dataPin, int latchPin, int clockPin){
uint8_t Pin_byte1 = 0b11111111; // For pins 0-7
uint8_t Pin_byte2 = 0b11111111; // For pins 8-15
uint8_t Pin_byte3 = 0b11111111; // For pins 16-23
// Update the correct byte based on Pin
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte3); // Send byte for pins 16-23
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_void I2C_RST(int dataPin, int latchPin, int clockPin){
uint8_t Pin_byte1 = 0b11111111; // For pins 0-7
uint8_t Pin_byte2 = 0b11111111; // For pins 8-15
uint8_t Pin_byte3 = 0b11111111; // For pins 16-23
// Update the correct byte based on Pin
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte3); // Send byte for pins 16-23
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin, HIGH);
delay(200);
}
void LPn0(uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin0, LOW);
// Shift out the bits
shiftOut(dataPin0, clockPin0, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin0, clockPin0, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin0, HIGH);
}
void LPn1(uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin1, LOW);
// Shift out the bits
shiftOut(dataPin1, clockPin1, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin1, clockPin1, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin1, HIGH);
}byte1); // Send byte for pins 0-7
digitalWrite(latchPin, HIGH);
delay(200);
}
void LPn0(uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin0, LOW);
// Shift out the bits
shiftOut(dataPin0, clockPin0, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin0, clockPin0, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin0, HIGH);
}
void LPn1(uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin1, LOW);
// Shift out the bits
shiftOut(dataPin1, clockPin1, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin1, clockPin1, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin1, HIGH);
} }
} }
} }
......
#include <Arduino.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <vl53l7cx_class.h>
#include "74HC154.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#define DEV_I2C1 Wire1
#define DEV_I2C0 Wire
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#define LedPin LED_BUILTIN
#define Fake_I2C_RST_PIN 99
#define Fake_LPN 99
#define LPN_PIN0 0
#define LPN_PIN1 1
#define LPN_PIN2 2
#define LPN_PIN3 3
#define LPN_PIN4 4
#define LPN_PIN5 5
#define LPN_PIN6 6
#define LPN_PIN7 7
#define LPN_PIN8 8
#define LPN_PIN9 9
#define LPN_PIN10 10
#define LPN_PIN11 11
#define LPN_PIN12 12
#define LPN_PIN13 13
#define LPN_PIN14 14
#define LPN_PIN15 15
#define LPN_PIN16 16
#define LPN_PIN17 17
pin_size_t dataPin0 = 18;
pin_size_t latchPin0 = 17;
pin_size_t clockPin0 = 16;
pin_size_t dataPin1 = 13;
pin_size_t latchPin1 = 14;
pin_size_t clockPin1 = 15;
uint16_t sensoraddress0 = 0x30;
uint16_t sensoraddress1 = 0x31;
uint16_t sensoraddress2 = 0x32;
uint16_t sensoraddress3 = 0x33;
uint16_t sensoraddress4 = 0x34;
uint16_t sensoraddress5 = 0x35;
uint16_t sensoraddress6 = 0x36;
uint16_t sensoraddress7 = 0x37;
uint16_t sensoraddress8 = 0x38;
uint16_t sensoraddress9 = 0x40;
uint16_t sensoraddress10 = 0x41;
uint16_t sensoraddress11 = 0x42;
uint16_t sensoraddress12 = 0x43;
uint16_t sensoraddress13 = 0x44;
uint16_t sensoraddress14 = 0x45;
uint16_t sensoraddress15 = 0x46;
uint16_t sensoraddress16 = 0x47;
uint16_t sensoraddress17 = 0x48;
uint16_t wait_for_i2c = 50;
uint16_t imageWidth = 8;
uint16_t ranging_frequency = 15;
pin_size_t SCL_PIN0 = 21;
pin_size_t SDA_PIN0 = 20;
pin_size_t SDA_PIN1 = 2;
pin_size_t SCL_PIN1 = 3;
JsonDocument doc;
// Components.
VL53L7CX sensor0(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor1(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor2(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor3(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor4(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor5(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor6(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor7(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor8(&DEV_I2C0, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor9(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor10(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor11(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor12(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor13(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor14(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor15(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor16(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
VL53L7CX sensor17(&DEV_I2C1, Fake_LPN, Fake_I2C_RST_PIN);
void blink_led_loop(void);
void setup() {
// put your setup code here, to run once:
delay(7000);
pinMode(dataPin0, OUTPUT);
pinMode(latchPin0, OUTPUT);
pinMode(clockPin0, OUTPUT);
pinMode(dataPin1, OUTPUT);
pinMode(latchPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
Serial.begin(115200);
Serial.println("Initialize... Please wait, it may take few seconds...");
// Initialize I2C bus.
DEV_I2C0.setSCL(SCL_PIN0);
DEV_I2C0.setSDA(SDA_PIN0);
DEV_I2C0.begin();
DEV_I2C0.setClock(1000000);
delay(50);
DEV_I2C1.setSCL(SCL_PIN1);
DEV_I2C1.setSDA(SDA_PIN1);
DEV_I2C1.begin();
DEV_I2C1.setClock(1000000);
delay(50);
// Initialize and configure sensors
Serial.println("Initializing sensors...");
initializeSensorInterface0(sensor0, sensoraddress0, LPN_PIN0);
i2cScanner(); // Perform I2C scan
initializeSensorInterface0(sensor1, sensoraddress1, LPN_PIN1);
i2cScanner(); // Perform I2C scan
initializeSensorInterface0(sensor2, sensoraddress2, LPN_PIN2);
i2cScanner(); // Perform I2C scan
initializeSensorInterface0(sensor3, sensoraddress3, LPN_PIN3);
i2cScanner(); // Perform I2C scan
Serial.println("All sensors initialized successfully.");
}
void loop() {
// Declare the result data variables for each sensor
VL53L7CX_ResultsData Results0;
VL53L7CX_ResultsData Results1;
VL53L7CX_ResultsData Results2;
VL53L7CX_ResultsData Results3;
// Process each sensor data and save to respective JSON arrays
processSensorData(sensor0, Results0, doc["sensor0"].to<JsonArray>(), "sensor0");
processSensorData(sensor1, Results1, doc["sensor1"].to<JsonArray>(), "sensor1");
processSensorData(sensor2, Results2, doc["sensor2"].to<JsonArray>(), "sensor2");
processSensorData(sensor3, Results3, doc["sensor3"].to<JsonArray>(), "sensor3");
// Serialize the JSON document and print to Serial
serializeJson(doc, Serial);
Serial.println();
}
void LPn(uint16_t dataPin, uint16_t latchPin, uint16_t clockPin, uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
Serial.print(Pin_byte1,BIN);
Serial.println(Pin_byte2,BIN);
digitalWrite(latchPin, HIGH);
}
void I2C_RST(uint16_t dataPin, uint16_t latchPin, uint16_t clockPin){
uint8_t Pin_byte1 = 0b11111111; // For pins 0-7
uint8_t Pin_byte2 = 0b11111111; // For pins 8-15
// Update the correct byte based on Pin
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin, HIGH);
delay(200);
}
void processSensorData(VL53L7CX &sensor, VL53L7CX_ResultsData &results, const JsonArray& data, const char* sensorKey) {
uint8_t NewDataReady = 0;
uint8_t status;
// Wait for data to be ready
do {
status = sensor.vl53l7cx_check_data_ready(&NewDataReady);
} while (!NewDataReady);
// Turn LED on to indicate data processing
digitalWrite(LedPin, HIGH);
// If data is ready, get the ranging data and store it in the JSON array
if ((!status) && (NewDataReady != 0)) {
status = sensor.vl53l7cx_get_ranging_data(&results);
JsonArray sensorData = doc[sensorKey].to<JsonArray>();
// Process the results and add them to the JSON array
for (int y = imageWidth * (imageWidth - 1); y >= 0; y -= imageWidth) {
for (int x = 0; x <= imageWidth - 1; x++) {
sensorData.add(results.distance_mm[VL53L7CX_NB_TARGET_PER_ZONE * (x + y)]);
}
}
}
// Turn LED off to indicate processing is done
digitalWrite(LedPin, LOW);
}
void initializeSensorInterface0(VL53L7CX &sensor, uint16_t sensorAddress, int lpnPin) {
LPn(dataPin0, latchPin0, clockPin0, lpnPin, true); // Activate sensor power
delay(wait_for_i2c);
sensor.begin();
delay(wait_for_i2c);
sensor.init_sensor();
delay(wait_for_i2c);
// Set I2C address
sensor.vl53l7cx_set_i2c_address(sensorAddress << 1);
delay(wait_for_i2c);
// Set resolution and frequency
sensor.vl53l7cx_set_resolution(VL53L7CX_RESOLUTION_8X8);
delay(wait_for_i2c);
sensor.vl53l7cx_set_ranging_frequency_hz(ranging_frequency);
delay(wait_for_i2c);
// Start ranging
sensor.vl53l7cx_start_ranging();
delay(wait_for_i2c);
// Reset LPN and I2C pins
LPn(dataPin0, latchPin0, clockPin0, lpnPin, false);
}
void initializeSensorInterface1(VL53L7CX &sensor, uint16_t sensorAddress, int lpnPin) {
LPn(dataPin1, latchPin1, clockPin1, lpnPin, true); // Activate sensor power
delay(wait_for_i2c);
sensor.begin();
delay(wait_for_i2c);
sensor.init_sensor();
delay(wait_for_i2c);
// Set I2C address
sensor.vl53l7cx_set_i2c_address(sensorAddress << 1);
delay(wait_for_i2c);
// Set resolution and frequency
sensor.vl53l7cx_set_resolution(VL53L7CX_RESOLUTION_8X8);
delay(wait_for_i2c);
sensor.vl53l7cx_set_ranging_frequency_hz(ranging_frequency);
delay(wait_for_i2c);
// Start ranging
sensor.vl53l7cx_start_ranging();
delay(wait_for_i2c);
// Reset LPN and I2C pins
LPn(dataPin1, latchPin1, clockPin1, lpnPin, false);
}
void i2cScanner()
{
for (byte address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0)
{
Serial.print("Device found at address 0x");
if (address < 0x10)
Serial.print("0");
Serial.println(address, HEX);
}
}
Serial.println();
delay(wait_for_i2c);
}
void blink_led_loop(void)
{
do {
// Blink the led forever
digitalWrite(LedPin, HIGH);
delay(wait_for_i2c);
digitalWrite(LedPin, LOW);
} while (1);
}
\ No newline at end of file
#include <Arduino.h>
#include <Wire.h>
#include <vl53l5cx_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#define DEV_I2C Wire
#define SerialPort Serial
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#define LedPin LED_BUILTIN
#define LPN_PIN 99
#define I2C_RST_PIN 99
#define PWREN_PIN 99
// Components.
VL53L5CX sensor_vl53l5cx_sat(&DEV_I2C, LPN_PIN, I2C_RST_PIN);
void blink_led_loop(void);
void blink_led_loop(void)
{
do {
// Blink the led forever
digitalWrite(LedPin, HIGH);
delay(500);
digitalWrite(LedPin, LOW);
} while (1);
}
/* Setup ---------------------------------------------------------------------*/
void setup()
{
}
void loop()
{
}
File added
uint16_t dataPin = 18;
uint16_t latchPin = 17;
uint16_t clockPin = 16;
#define LPN_PIN0 0
#define LPN_PIN1 1
#define LPN_PIN2 2
#define LPN_PIN3 3
void setup() {
// put your setup code here, to run once:
delay(7000);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
LPn(dataPin, latchPin, clockPin, 1, true); // Activate sensor I2C
Ranging(dataPin, latchPin, clockPin)
}
void loop() {
// put your main code here, to run repeatedly:
}
void LPn(uint16_t dataPin, uint16_t latchPin, uint16_t clockPin, uint16_t Pin, bool on) {
uint8_t Pin_byte1 = 0b00000000; // For pins 0-7
uint8_t Pin_byte2 = 0b00000000; // For pins 8-15
// Update the correct byte based on Pin
if (Pin <= 7) {
Pin_byte1 |= (on << Pin); // Set the corresponding bit in Pin_byte1
} else if (Pin <= 15) {
Pin_byte2 |= (on << (Pin - 8)); // Set the corresponding bit in Pin_byte2
}
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin, HIGH);
}
void Ranging(uint16_t dataPin, uint16_t latchPin, uint16_t clockPin){
uint8_t Pin_byte1 = 0b11111111; // For pins 0-7
uint8_t Pin_byte2 = 0b11111111; // For pins 8-15
// Update the correct byte based on Pin
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte2); // Send byte for pins 8-15
shiftOut(dataPin, clockPin, MSBFIRST, Pin_byte1); // Send byte for pins 0-7
digitalWrite(latchPin, HIGH);
delay(200);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment