Initial Commit
This commit is contained in:
123
sencure.ino
Normal file
123
sencure.ino
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
|
const int deviceAddress = 0x68; // I2C address of the device (change as needed)
|
||||||
|
const uint8_t relay1 = 2;
|
||||||
|
const uint8_t relay2 = 3;
|
||||||
|
const uint8_t oe = 5;
|
||||||
|
const uint8_t d6 = 6; // Define pin D6
|
||||||
|
bool actionPerformed = false; // Flag to track if the action has been performed
|
||||||
|
|
||||||
|
// Function to transmit 16-bit data in little-endian format
|
||||||
|
void transmitData(uint8_t registerAddress, uint16_t data) {
|
||||||
|
Wire.beginTransmission(deviceAddress);
|
||||||
|
Wire.write(registerAddress); // Send the register address
|
||||||
|
Wire.write(data & 0xFF); // Send the low byte (LSB)
|
||||||
|
Wire.write((data >> 8) & 0xFF); // Send the high byte (MSB)
|
||||||
|
Wire.endTransmission();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to read back data from the register
|
||||||
|
uint16_t readData(uint8_t registerAddress) {
|
||||||
|
uint16_t receivedData = 0;
|
||||||
|
|
||||||
|
// First, send the register address to read from
|
||||||
|
Wire.beginTransmission(deviceAddress);
|
||||||
|
Wire.write(registerAddress); // Send the register address
|
||||||
|
Wire.endTransmission();
|
||||||
|
if (Wire.endTransmission() != 0) {
|
||||||
|
Serial.println("Device not found");
|
||||||
|
return 0; // Return 0 or some error code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now request 2 bytes from the device
|
||||||
|
Wire.requestFrom(deviceAddress, 2);
|
||||||
|
if (Wire.available() == 2) {
|
||||||
|
receivedData |= Wire.read(); // Read low byte (LSB)
|
||||||
|
receivedData |= Wire.read() << 8; // Read high byte (MSB)
|
||||||
|
}
|
||||||
|
return receivedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
Wire.begin(); // Initialize I2C
|
||||||
|
pinMode(relay1,OUTPUT);
|
||||||
|
pinMode(relay2,OUTPUT);
|
||||||
|
pinMode(d6, INPUT); // Set D6 as input
|
||||||
|
|
||||||
|
for(int i = 8; i <= 13; i++)
|
||||||
|
{
|
||||||
|
pinMode(i,OUTPUT);
|
||||||
|
digitalWrite(i,LOW);
|
||||||
|
}
|
||||||
|
pinMode(oe,OUTPUT);
|
||||||
|
digitalWrite(relay1,LOW);
|
||||||
|
digitalWrite(relay2,LOW);
|
||||||
|
digitalWrite(oe,HIGH);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void printHex(uint16_t value) {
|
||||||
|
Serial.print("0x");
|
||||||
|
if (value < 0x10) {
|
||||||
|
Serial.print("000");
|
||||||
|
} else if (value < 0x100) {
|
||||||
|
Serial.print("00");
|
||||||
|
} else if (value < 0x1000) {
|
||||||
|
Serial.print("0");
|
||||||
|
}
|
||||||
|
Serial.println(value, HEX); // Print the value in hexadecimal
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long lastDebounceTime = 0; // Last time the pin state changed
|
||||||
|
unsigned long debounceDelay = 50; // Debounce delay in milliseconds
|
||||||
|
int lastD6State = LOW; // Last stable state of D6
|
||||||
|
int currentD6State; // Current state of D6
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Read the current state of D6
|
||||||
|
int reading = digitalRead(d6);
|
||||||
|
|
||||||
|
// Check if the state has changed
|
||||||
|
if (reading != lastD6State) {
|
||||||
|
// Reset the debounce timer
|
||||||
|
lastDebounceTime = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the state has been stable for the debounce delay, update the state
|
||||||
|
if ((millis() - lastDebounceTime) > debounceDelay) {
|
||||||
|
// Only proceed if the state has changed
|
||||||
|
if (reading != currentD6State) {
|
||||||
|
currentD6State = reading;
|
||||||
|
|
||||||
|
// Only act if the new state is HIGH and the action hasn't been performed yet
|
||||||
|
if (currentD6State == HIGH && !actionPerformed) {
|
||||||
|
uint8_t registerAddress = 0xBF;
|
||||||
|
uint16_t dataToSend = 0x0000;
|
||||||
|
|
||||||
|
// Transmit the data
|
||||||
|
transmitData(registerAddress, dataToSend); // Transmit data
|
||||||
|
// Read back data
|
||||||
|
uint16_t receivedData = readData(registerAddress); // Read back data
|
||||||
|
|
||||||
|
// Use the method to print received data in hexadecimal format
|
||||||
|
Serial.println("Received data : ");
|
||||||
|
printHex(receivedData); // Print formatted received data
|
||||||
|
Serial.println(); // New line for better readability
|
||||||
|
|
||||||
|
// Set the action performed flag to true
|
||||||
|
actionPerformed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the state is LOW, reset the action performed flag
|
||||||
|
if (currentD6State == LOW) {
|
||||||
|
actionPerformed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the last stable state
|
||||||
|
lastD6State = reading;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user