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

added new arduino sketch

parent 13b04078
No related branches found
No related tags found
No related merge requests found
Showing
with 1059 additions and 28 deletions
/**
******************************************************************************
* @file VL53L7CX_Sat_HelloWorld.ino
* @author STMicroelectronics
* @version V1.0.0
* @date 11 November 2021
* @brief Arduino test application for the STMicrolectronics VL53L7CX
* proximity sensor satellite based on FlightSense.
* This application makes use of C++ classes obtained from the C
* components' drivers.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2021 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/*
* To use these examples you need to connect the VL53L7CX satellite sensor directly to the Nucleo board with wires as explained below:
* pin 1 (GND) of the VL53L7CX satellite connected to GND of the Nucleo board
* pin 2 (IOVDD) of the VL53L7CX satellite connected to 3V3 pin of the Nucleo board
* pin 3 (AVDD) of the VL53L7CX satellite connected to 5V pin of the Nucleo board
* pin 4 (PWREN) of the VL53L7CX satellite connected to pin A5 of the Nucleo board
* pin 5 (LPn) of the VL53L7CX satellite connected to pin A3 of the Nucleo board
* pin 6 (SCL) of the VL53L7CX satellite connected to pin D15 (SCL) of the Nucleo board
* pin 7 (SDA) of the VL53L7CX satellite connected to pin D14 (SDA) of the Nucleo board
* pin 8 (I2C_RST) of the VL53L7CX satellite connected to pin A1 of the Nucleo board
* pin 9 (INT) of the VL53L7CX satellite connected to pin A2 of the Nucleo board
*/
/* Includes ------------------------------------------------------------------*/
#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 LPN_PIN0 14
#define LPN_PIN1 15
#define LPN_PIN2 9
#define LPN_PIN3 12
#define LPN_PIN4 99
#define LPN_PIN5 99
#define LPN_PIN6 99
#define LPN_PIN7 99
#define LPN_PIN8 99
#define LPN_PIN9 99
#define LPN_PIN10 99
#define LPN_PIN11 99
#define LPN_PIN12 99
#define LPN_PIN13 99
#define LPN_PIN14 99
#define LPN_PIN15 99
#define LPN_PIN16 99
#define LPN_PIN17 99
#define LPN_PIN1 15
#define I2C_RST_PIN 27
#define LPN_PIN2 9
uint16_t dataPin0 = 18;
uint16_t latchPin0 = 17;
uint16_t clockPin0 = 16;
uint16_t dataPin1 = 13;
uint16_t latchPin1 = 14;
uint16_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 = 0x39;
uint16_t sensoraddress10 = 0x3A;
uint16_t sensoraddress11 = 0x3B;
uint16_t sensoraddress12 = 0x3C;
uint16_t sensoraddress13 = 0x3D;
uint16_t sensoraddress14 = 0x3E;
uint16_t sensoraddress15 = 0x3F;
uint16_t sensoraddress16 = 0x40;
uint16_t sensoraddress17 = 0x41;
uint16_t wait_for_i2c = 50;
uint16_t imageWidth = 8;
uint16_t ranging_frequency = 15;
pin_size_t SDA_PIN1 = 2;
pin_size_t SCL_PIN1 = 3;
JsonDocument doc;
// Components.
VL53L7CX sensor0(&DEV_I2C0, LPN_PIN0, I2C_RST_PIN);
VL53L7CX sensor1(&DEV_I2C0, LPN_PIN1, I2C_RST_PIN);
VL53L7CX sensor2(&DEV_I2C0, LPN_PIN2, I2C_RST_PIN);
VL53L7CX sensor3(&DEV_I2C0, LPN_PIN3, I2C_RST_PIN);
VL53L7CX sensor4(&DEV_I2C0, LPN_PIN4, I2C_RST_PIN);
VL53L7CX sensor5(&DEV_I2C0, LPN_PIN5, I2C_RST_PIN);
VL53L7CX sensor6(&DEV_I2C0, LPN_PIN6, I2C_RST_PIN);
VL53L7CX sensor7(&DEV_I2C0, LPN_PIN7, I2C_RST_PIN);
VL53L7CX sensor8(&DEV_I2C0, LPN_PIN8, I2C_RST_PIN);
VL53L7CX sensor9(&DEV_I2C1, LPN_PIN9, I2C_RST_PIN);
VL53L7CX sensor10(&DEV_I2C1, LPN_PIN10, I2C_RST_PIN);
VL53L7CX sensor11(&DEV_I2C1, LPN_PIN11, I2C_RST_PIN);
VL53L7CX sensor12(&DEV_I2C1, LPN_PIN12, I2C_RST_PIN);
VL53L7CX sensor13(&DEV_I2C1, LPN_PIN13, I2C_RST_PIN);
VL53L7CX sensor14(&DEV_I2C1, LPN_PIN14, I2C_RST_PIN);
VL53L7CX sensor15(&DEV_I2C1, LPN_PIN15, I2C_RST_PIN);
VL53L7CX sensor16(&DEV_I2C1, LPN_PIN16, I2C_RST_PIN);
VL53L7CX sensor17(&DEV_I2C1, LPN_PIN17, I2C_RST_PIN);
void blink_led_loop(void);
void blink_led_loop(void)
{
do {
// Blink the led forever
digitalWrite(LedPin, HIGH);
delay(wait_for_i2c);
digitalWrite(LedPin, LOW);
} while (1);
}
/* Setup ---------------------------------------------------------------------*/
void setup()
{
// Led.
pinMode(LedPin, OUTPUT);
pinMode(LPN_PIN0, OUTPUT);
pinMode(LPN_PIN1, OUTPUT);
pinMode(LPN_PIN2, OUTPUT);
pinMode(LPN_PIN3, OUTPUT);
Serial.begin(115200);
Serial.println("Initialize... Please wait, it may take few seconds...");
// Initialize I2C bus.
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...");
i2cScanner();
initializeSensor(sensor0, sensoraddress0, LPN_PIN0, I2C_RST_PIN);
i2cScanner();
initializeSensor(sensor1, sensoraddress1, LPN_PIN1, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan
initializeSensor(sensor2, sensoraddress2, LPN_PIN2, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan
initializeSensor(sensor3, sensoraddress3, LPN_PIN3, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan
// initializeSensor(sensor4, sensoraddress4, LPN_PIN4, I2C_RST_PIN);
// i2cScanner();
// initializeSensor(sensor5, sensoraddress5, LPN_PIN5, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor6, sensoraddress6, LPN_PIN6, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor7, sensoraddress7, LPN_PIN7, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor8, sensoraddress8, LPN_PIN8, I2C_RST_PIN);
// i2cScanner();
// initializeSensor(sensor9, sensoraddress9, LPN_PIN9, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor10, sensoraddress10, LPN_PIN10, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor11, sensoraddress11, LPN_PIN11, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor12, sensoraddress12, LPN_PIN12, I2C_RST_PIN);
// i2cScanner();
// initializeSensor(sensor13, sensoraddress13, LPN_PIN13, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor14, sensoraddress14, LPN_PIN14, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor15, sensoraddress15, LPN_PIN15, I2C_RST_PIN);
// i2cScanner(); // Perform I2C scan
// initializeSensor(sensor16, sensoraddress16, LPN_PIN16, I2C_RST_PIN);
// i2cScanner();
// initializeSensor(sensor17, sensoraddress17, LPN_PIN17, I2C_RST_PIN);
// 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();
}
bool i2cScanner()
{
bool status = false;
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);
if (address == 0x29){
status = true;
}
else{
status = false;
}
}
}
Serial.println();
delay(wait_for_i2c);
return status;
}
void initializeSensor(VL53L7CX &sensor, uint16_t sensorAddress, int lpnPin, int i2cRstPin) {
LPn0(lpnPin, true);
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
LPn0(lpnPin, false);
}
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 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);
}
uint8_t LPN_PIN0 = 0b00000001;
uint8_t LPN_PIN1 = 0b00000010;
uint8_t LPN_PIN2 = 0b00000100;
uint8_t LPN_PIN3 = 0b00001000;
uint8_t LPN_PIN4 = 0b00010000;
uint8_t LPN_PIN5 = 0b00100000;
uint8_t LPN_PIN6 = 0b01000000;
uint8_t LPN_PIN7 = 0b10000000;
uint8_t LPN_PIN8 = 0b00000001;
uint8_t I2C_RST0 = 0b00000010;
uint8_t I2C_RST1 = 0b00000100;
uint8_t I2C_RST2 = 0b00001000;
uint8_t I2C_RST3 = 0b00010000;
uint8_t I2C_RST4 = 0b00100000;
uint8_t I2C_RST5 = 0b01000000;
uint8_t I2C_RST6 = 0b10000000;
uint8_t I2C_RST7 = 0b00000001;
uint8_t I2C_RST8 = 0b00000010;
uint8_t LPN_PIN9 = 0b00000010;
uint8_t LPN_PIN10 = 0b00000100;
uint8_t LPN_PIN11 = 0b00001000;
uint8_t LPN_PIN12 = 0b00010000;
uint8_t LPN_PIN13 = 0b00100000;
uint8_t LPN_PIN14 = 0b01000000;
uint8_t LPN_PIN15 = 0b10000000;
uint8_t LPN_PIN16 = 0b00000001;
uint8_t LPN_PIN17 = 0b00000001;
uint8_t I2C_RST9 = 0b00000010;
uint8_t I2C_RST10 = 0b00000100;
uint8_t I2C_RST11 = 0b00001000;
uint8_t I2C_RST12 = 0b00010000;
uint8_t I2C_RST13 = 0b00100000;
uint8_t I2C_RST14 = 0b01000000;
uint8_t I2C_RST15 = 0b10000000;
uint8_t I2C_RST16 = 0b00000001;
uint8_t I2C_RST17 = 0b00000001;
//Pin connected to ST_CP of 74HC595
int latchPin = 16;
//Pin connected to SH_CP of 74HC595
int clockPin = 17;
////Pin connected to DS of 74HC595
int dataPin = 18;
uint8_t Pin_byte = 0b00000001;
uint16_t Pin = 0;
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void I2C_RST(){
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 LPn(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 loop(){
LPn(Pin, false);
Pin++;
if(Pin >= 16){
Pin = 0;
I2C_RST();
}
delay(100);
}
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include <Wire.h> #include <Wire.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <vl53l7cx_class.h> #include <vl53l7cx_class.h>
#include "74HC154.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
...@@ -70,22 +71,18 @@ ...@@ -70,22 +71,18 @@
#define LedPin LED_BUILTIN #define LedPin LED_BUILTIN
#define LPN_PIN0 14 #define LPN_PIN0 14
#define I2C_RST_PIN0 27 #define LPN_PIN1 15
#define PWREN_PIN 27 #define LPN_PIN2 9
#define LPN_PIN3 12
#define LPN_PIN1 15 #define LPN_PIN1 15
#define I2C_RST_PIN1 27 #define I2C_RST_PIN 27
#define PWREN_PIN 27
#define LPN_PIN2 9 #define LPN_PIN2 9
#define I2C_RST_PIN2 27
#define PWREN_PIN 27
#define LPN_PIN3 12
#define I2C_RST_PIN3 27
#define PWREN_PIN 27
uint16_t sensoraddress0 = 0x1B;
uint16_t sensoraddress0 = 0x1Bz;
uint16_t sensoraddress1 = 0x39; uint16_t sensoraddress1 = 0x39;
uint16_t sensoraddress2 = 0x57; uint16_t sensoraddress2 = 0x57;
uint16_t sensoraddress3 = 0x67; uint16_t sensoraddress3 = 0x67;
...@@ -98,10 +95,10 @@ pin_size_t SCL_PIN1 = 3; ...@@ -98,10 +95,10 @@ pin_size_t SCL_PIN1 = 3;
JsonDocument doc; JsonDocument doc;
// Components. // Components.
VL53L7CX sensor0(&DEV_I2C1, LPN_PIN0, I2C_RST_PIN0); VL53L7CX sensor0(&DEV_I2C1, LPN_PIN0, I2C_RST_PIN);
VL53L7CX sensor1(&DEV_I2C0, LPN_PIN1, I2C_RST_PIN1); VL53L7CX sensor1(&DEV_I2C0, LPN_PIN1, I2C_RST_PIN);
VL53L7CX sensor2(&DEV_I2C0, LPN_PIN2, I2C_RST_PIN2); VL53L7CX sensor2(&DEV_I2C0, LPN_PIN2, I2C_RST_PIN);
VL53L7CX sensor3(&DEV_I2C0, LPN_PIN2, I2C_RST_PIN2); VL53L7CX sensor3(&DEV_I2C0, LPN_PIN2, I2C_RST_PIN);
void blink_led_loop(void); void blink_led_loop(void);
...@@ -125,17 +122,21 @@ void setup() ...@@ -125,17 +122,21 @@ void setup()
pinMode(LPN_PIN1, OUTPUT); pinMode(LPN_PIN1, OUTPUT);
pinMode(LPN_PIN2, OUTPUT); pinMode(LPN_PIN2, OUTPUT);
pinMode(LPN_PIN3, OUTPUT); pinMode(LPN_PIN3, OUTPUT);
pinMode(LPN_PIN4, OUTPUT);
pinMode(LPN_PIN5, OUTPUT);
pinMode(LPN_PIN6, OUTPUT);
pinMode(LPN_PIN7, OUTPUT);
pinMode(LPN_PIN8, OUTPUT);
pinMode(LPN_PIN9, OUTPUT);
pinMode(LPN_PIN10, OUTPUT);
pinMode(LPN_PIN11, OUTPUT);
pinMode(LPN_PIN12, OUTPUT);
pinMode(LPN_PIN13, OUTPUT);
pinMode(LPN_PIN14, OUTPUT);
pinMode(LPN_PIN15, OUTPUT);
pinMode(LPN_PIN16, OUTPUT);
pinMode(LPN_PIN17, OUTPUT);
// digitalWrite(LPN_PIN0, HIGH);
// digitalWrite(LPN_PIN1, HIGH);
// digitalWrite(LPN_PIN2, HIGH);
// digitalWrite(LPN_PIN3, HIGH);
// delay(500);
// digitalWrite(LPN_PIN0, LOW);
// digitalWrite(LPN_PIN1, LOW);
// digitalWrite(LPN_PIN2, LOW);
// digitalWrite(LPN_PIN3, LOW);
// Initialize serial for output.
Serial.begin(115200); Serial.begin(115200);
Serial.println("Initialize... Please wait, it may take few seconds..."); Serial.println("Initialize... Please wait, it may take few seconds...");
...@@ -153,16 +154,16 @@ void setup() ...@@ -153,16 +154,16 @@ void setup()
// Initialize and configure sensors // Initialize and configure sensors
Serial.println("Initializing sensors..."); Serial.println("Initializing sensors...");
initializeSensor(sensor0, sensoraddress0, LPN_PIN0, I2C_RST_PIN0); initializeSensor(sensor0, sensoraddress0, LPN_PIN0, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan i2cScanner(); // Perform I2C scan
initializeSensor(sensor1, sensoraddress1, LPN_PIN1, I2C_RST_PIN1); initializeSensor(sensor1, sensoraddress1, LPN_PIN1, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan i2cScanner(); // Perform I2C scan
initializeSensor(sensor2, sensoraddress2, LPN_PIN2, I2C_RST_PIN2); initializeSensor(sensor2, sensoraddress2, LPN_PIN2, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan i2cScanner(); // Perform I2C scan
initializeSensor(sensor3, sensoraddress3, LPN_PIN3, I2C_RST_PIN3); initializeSensor(sensor3, sensoraddress3, LPN_PIN3, I2C_RST_PIN);
i2cScanner(); // Perform I2C scan i2cScanner(); // Perform I2C scan
Serial.println("All sensors initialized successfully."); Serial.println("All sensors initialized successfully.");
......
#pragma once
//
// FILE: 74HC154.h
// AUTHOR: Rob Tillaart
// DATE: 2024-02-22
// VERSION: 0.1.0
// PURPOSE: Arduino library for the 74HC154 4-to-16 line decoder/demultiplexer.
// URL: https://github.com/RobTillaart/74HC154
#include "Arduino.h"
#define LIB_74HC154_VERSION (F("0.1.0"))
class DEV_74HC154
{
public:
DEV_74HC154(uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pinEnable = 255)
{
_pin[0] = pin0;
_pin[1] = pin1;
_pin[2] = pin2;
_pin[3] = pin3;
_enable = pinEnable;
_line = 0;
for (int i = 0; i < 4; i++)
{
pinMode(_pin[i], OUTPUT);
digitalWrite(_pin[i], LOW);
}
if (pinEnable != 255)
{
// default disable.
pinMode(_enable, OUTPUT);
digitalWrite(_enable, HIGH);
}
}
DEV_74HC154(uint8_t * pins, uint8_t pinEnable = 255)
{
_line = 0;
for (int i = 0; i < 4; i++)
{
_pin[i] = pins[i];
pinMode(_pin[i], OUTPUT);
digitalWrite(_pin[i], LOW);
}
if (pinEnable != 255)
{
// default disable.
pinMode(_enable, OUTPUT);
digitalWrite(_enable, HIGH);
}
}
// nr == 0..15
bool setLine(uint8_t nr)
{
if (nr > 15) return false;
_line = nr;
_setLine();
return true;
}
uint8_t getLine()
{
return _line;
}
void nextLine()
{
if (_line >= 15) _line = 0;
else _line++;
_setLine();
}
void prevLine()
{
if (_line == 0) _line = 15;
else _line--;
_setLine();
}
void enable()
{
digitalWrite(_enable, LOW);
}
void disable()
{
digitalWrite(_enable, HIGH);
}
bool isEnabled()
{
return digitalRead(_enable);
}
private:
uint8_t _pin[4];
uint8_t _enable = 255;
uint8_t _line = 0;
void _setLine()
{
digitalWrite(_pin[0], _line & 0x01);
digitalWrite(_pin[1], _line & 0x02);
digitalWrite(_pin[2], _line & 0x04);
digitalWrite(_pin[3], _line & 0x08);
}
/*
uint8_t getLine()
{
uint8_t value = digitalRead(_pin[0]);
value |= digitalRead(_pin[1]) << 1;
value |= digitalRead(_pin[2]) << 2;
value |= digitalRead(_pin[3]) << 4;
return value;
};
*/
};
// -- END OF FILE --
# Change Log DEV_74HC154
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.0] - 2024-02-23
- initial version
MIT License
Copyright (c) 2024-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
[![Arduino CI](https://github.com/RobTillaart/74HC154/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/74HC154/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/74HC154/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/74HC154/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/74HC154/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/74HC154.svg)](https://github.com/RobTillaart/74HC154/issues)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/74HC154/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/74HC154.svg?maxAge=3600)](https://github.com/RobTillaart/74HC154/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/74HC154.svg)](https://registry.platformio.org/libraries/robtillaart/74HC154)
# 74HC154
Arduino library for the 74HC154 4-to-16 line decoder/demultiplexer.
## Description
This library controls the 74HC154 4 to 6 line decoder.
With 4 IO lines one can select one of 16 output lines.
#### Related
- https://github.com/RobTillaart/74HC138 (3 to 8 selector)
- https://github.com/RobTillaart/74HC154 (4 to 16 selector)
- https://github.com/RobTillaart/HC4051 (1x8 mux)
- https://github.com/RobTillaart/HC4052 (2x4 mux)
- https://github.com/RobTillaart/HC4053 (3x2 mux)
- https://github.com/RobTillaart/HC4067 (1x16 mux)
## Interface
```cpp
#include "74HC154.h"
```
#### Constructor
- **74HC154(uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pinEnable = 255)**
set the 4 selection IO lines from pin numbers.
Optionally set the enable pin, connect to E1 or E2, see datasheet.
- **74HC154(uint8_t \* pins, uint8_t pinEnable = 255)**
set the 4 selection IO lines from an array.
The pins array should have at least 4 elements.
Optionally set the enable pin, connect to E1, see datasheet.
#### Select line
- **bool setLine(uint8_t line)** set line 0 .. 15. Returns false if out of range.
- **uint8_t getLine()** returns 0 .. 15
- **void nextLine()** selects the next line, wraps back to 0 if needed.
- **void prevLine()** selects the previous line, wraps to 15 if needed.
#### Enable
Works only if enable line is set in constructor.
- **void enable()** enables output / selection.
- **void disable()** disables output / selection.
- **bool isEnabled()** checks if line is enabled.
## Future
#### Must
- improve documentation
- get hardware to test
#### Should
#### Could
#### Wont
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,
//
// FILE: 74HC154_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test basic behaviour and performance
// URL: https://github.com/RobTillaart/
#include "74HC154.h"
DEV_74HC154 selector(5, 6, 7, 8);
uint8_t line = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LIB_74HC154_VERSION: ");
Serial.println(LIB_74HC154_VERSION);
selector.enable();
}
void loop()
{
Serial.println(selector.getLine());
delay(100);
selector.setLine(line);
line++;
if (line >= 16) line = 0;
delay(1000);
}
// -- END OF FILE --
//
// FILE: 74HC154_night_rider.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test basic behaviour and performance
// URL: https://github.com/RobTillaart/
#include "74HC154.h"
DEV_74HC154 selector(5, 6, 7, 8);
uint8_t line = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LIB_74HC154_VERSION: ");
Serial.println(LIB_74HC154_VERSION);
selector.enable();
}
void loop()
{
for (int i = 0; i < 16; i++)
{
Serial.println(selector.getLine());
selector.nextLine();
delay(100);
}
for (int i = 0; i < 16; i++)
{
Serial.println(selector.getLine());
selector.prevLine();
delay(100);
}
}
// -- END OF FILE --
//
// FILE: 74HC154_prev_next.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test basic behaviour and performance
// URL: https://github.com/RobTillaart/74HC154
#include "74HC154.h"
DEV_74HC154 selector(5, 6, 7, 8);
uint8_t line = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LIB_74HC154_VERSION: ");
Serial.println(LIB_74HC154_VERSION);
selector.enable();
}
void loop()
{
for (int i = 0; i < 30; i++)
{
Serial.println(selector.getLine());
selector.nextLine();
delay(100);
}
for (int i = 0; i < 30; i++)
{
Serial.println(selector.getLine());
selector.prevLine();
delay(100);
}
}
// -- END OF FILE --
# Syntax Colouring Map For DEV_74HC154
# Data types (KEYWORD1)
DEV_74HC154 KEYWORD1
# Methods and Functions (KEYWORD2)
setLine KEYWORD2
getLine KEYWORD2
nextLine KEYWORD2
prevLine KEYWORD2
enable KEYWORD2
disable KEYWORD2
isEnabled KEYWORD2
# Constants (LITERAL1)
LIB_74HC154_VERSION LITERAL1
{
"name": "74HC154",
"keywords": "3 to 8",
"description": "Arduino library for the 74HC154 4-to-16 line decoder/demultiplexer.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/74HC154.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
"headers": "74HC154.h"
}
name=74HC154
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the 74HC154 4 to 16 line decoder/demultiplexer.
paragraph=
category=Sensors
url=https://github.com/RobTillaart/74HC154
architectures=*
includes=74HC154.h
depends=
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2024-02-23
// PURPOSE: unit tests for the 74HC154 library
// URL: https://github.com/RobTillaart/74HC154
//
// supported assertions
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
// ----------------------------
// assertEqual(expected, actual)
// assertNotEqual(expected, actual)
// assertLess(expected, actual)
// assertMore(expected, actual)
// assertLessOrEqual(expected, actual)
// assertMoreOrEqual(expected, actual)
// assertTrue(actual)
// assertFalse(actual)
// assertNull(actual)
// assertNotNull(actual)
#include <ArduinoUnitTests.h>
#include "74HC154.h"
unittest_setup()
{
fprintf(stderr, "LIB_74HC154_VERSION: %s\n", (char *) LIB_74HC154_VERSION);
}
unittest_teardown()
{
}
unittest(test_setLine)
{
DEV_74HC154 dev(5, 6, 7, 8);
for (int i = 0; i < 16; i++)
{
assertTrue(dev.setLine(i));
assertEqual(i, dev.getLine());
}
assertFalse(dev.setLine(16));
}
unittest(test_nextLine)
{
DEV_74HC154 dev(5, 6, 7, 8);
assertTrue(dev.setLine(0));
for (int i = 0; i < 16; i++)
{
assertEqual(i, dev.getLine());
dev.nextLine();
}
}
unittest(test_prevLine)
{
DEV_74HC154 dev(5, 6, 7, 8);
assertTrue(dev.setLine(15));
for (int i = 15; i > 0; i--)
{
assertEqual(i, dev.getLine());
dev.prevLine();
}
}
unittest_main()
// -- END OF FILE --
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment