diff --git a/Examples/Example6_Change_I2C_Address/Example6_Change_I2C_Address.ino b/Examples/Example6_Change_I2C_Address/Example6_Change_I2C_Address.ino index f8a8697..0653589 100644 --- a/Examples/Example6_Change_I2C_Address/Example6_Change_I2C_Address.ino +++ b/Examples/Example6_Change_I2C_Address/Example6_Change_I2C_Address.ino @@ -1,19 +1,21 @@ /* - This example code demonstrates how to change the address of the Single or - Quad Qwiic Relay to one of your choosing. There is a set range of available - addresses from 0x07 to 0x78, so make sure your chosen address falls within - this range. The next thing to note is that when you change the address you'll - need to call an instance of the Qwiic_Relay class that includes your new - address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address - is fed to all the available functions. Finally if for some reason you've - forgotten your new address. No big deal, load up the I2C scanner example code - and find out where your product's address is at. - By: Elias Santistevan - SparkFun Electronics - Date: July 2019 - - License: This code is public domain but you buy me a beer if you use - this and we meet someday (Beerware license). +Example 6 Change I2C Address + +This example code demonstrates how to change the address of the Single or +Quad Qwiic Relay to one of your choosing. There is a set range of available +addresses from 0x07 to 0x78, so make sure your chosen address falls within +this range. The next thing to note is that when you change the address you'll +need to call an instance of the Qwiic_Relay class that includes your new +address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address +is fed to all the available functions. Finally if for some reason you've +forgotten your new address. No big deal, load up the I2C scanner example code +and find out where your product's address is at. +By: Elias Santistevan +SparkFun Electronics +Date: July 2019 + +License: This code is public domain but you buy me a beer if you use +this and we meet someday (Beerware license). */ #include @@ -23,33 +25,41 @@ // you want to access the alternate addresses. #define SINGLE_DEFAULT_ADDR 0x18 // Alternate jumper address 0x19 #define QUAD_DEFAULT_ADDR 0x6D // Alternate jumper address 0x6C - +#define NEW_ADDR 0x09 // After changing the address you'll need to apply that address to a new // instance of the Qwiic_Relay class: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE)". -Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using Quad Relay +Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using single relay void setup() { - Wire.begin(); - Serial.begin(115200); - - // Let's see..... - if(relay.begin()) - Serial.println("Ready to flip some switches."); - else - Serial.println("Check connections to Qwiic Relay."); - - // There is a not so limited but still limited range of - // addresses that are available to you 0x07 -> 0x78. - if(relay.changeAddress(0x09)) // Put the address you want here. - Serial.println("Address changed successfully."); - else - Serial.println("Address change failed..."); - - delay(2000); - - + Wire.begin(); + + Serial.begin(115200); + + if(!relay.begin()) + { + Serial.println("Can't communicate with Relay. Check wiring or that you have the correct I2C address."); + while(1) + ; + } + + Serial.println("Example 6 Changing the relay's I2C address."); + + // There is a not so limited but still limited range of + // addresses that are available to you 0x07 -> 0x78. + + // If you're using a single relay, indicate it by adding "true" as a second argument. + //if(relay.changeAddress(NEW_ADDR, true) + + if(relay.changeAddress(NEW_ADDR)) // Put the address you want here. + Serial.println("Address changed successfully."); + else + Serial.println("Address change failed..."); + + delay(2000); + + } void loop() diff --git a/src/SparkFun_Qwiic_Relay.cpp b/src/SparkFun_Qwiic_Relay.cpp index 8516d94..901e505 100644 --- a/src/SparkFun_Qwiic_Relay.cpp +++ b/src/SparkFun_Qwiic_Relay.cpp @@ -10,6 +10,8 @@ */ #include "SparkFun_Qwiic_Relay.h" +#include +#include Qwiic_Relay::Qwiic_Relay(uint8_t address) { @@ -66,7 +68,7 @@ uint8_t Qwiic_Relay::getState() // This function gets the version number of the SparkFun Single Relay. float Qwiic_Relay::singleRelayVersion() { - float version = _readVersion(FIRMWARE_VERSION); + float version = _readVersion(SFE_SINGLE_FIRMWARE_VERSION); return (version); } @@ -160,15 +162,24 @@ uint8_t Qwiic_Relay::getState(uint8_t relay) // This function changes the I-squared-C address of the Qwiic RFID. The address // is written to the memory location in EEPROM that determines its address. -bool Qwiic_Relay::changeAddress(uint8_t newAddress) +bool Qwiic_Relay::changeAddress(uint8_t newAddress, bool singleRelay) { if (newAddress < 0x07 || newAddress > 0x78) // Range of legal addresses return false; - _i2cPort->beginTransmission(_address); - _i2cPort->write(ADDRESS_LOCATION); - _i2cPort->write(newAddress); + if(singleRelay) + { + _i2cPort->beginTransmission(_address); + _i2cPort->write(SINGLE_CHANGE_ADDRESS); + _i2cPort->write(newAddress); + } + else + { + _i2cPort->beginTransmission(_address); + _i2cPort->write(QUAD_CHANGE_ADDRESS); + _i2cPort->write(newAddress); + } if (!_i2cPort->endTransmission()) return true; @@ -183,13 +194,9 @@ bool Qwiic_Relay::_writeAddress(uint8_t addressToWrite, uint8_t value) _i2cPort->write(addressToWrite); // Toggle it on.... _i2cPort->write(value); if (_i2cPort->endTransmission() != 0) - { return false; // Transaction failed - } // End communcation. else - { return true; - } } // This function handles I-squared-C write commands for turning the relays on. // The quad relay relies on the current state of the relay to determine whether @@ -203,9 +210,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command) { _status = _readCommand(RELAY_ONE_STATUS); if (_status == QUAD_RELAY_ON) - { // Is it on? Then.... return; // Do nothing.... - } else { // Off? _i2cPort->beginTransmission(_address); // Start communication. @@ -218,9 +223,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command) { _status = _readCommand(RELAY_TWO_STATUS); if (_status == QUAD_RELAY_ON) - { return; - } else { _i2cPort->beginTransmission(_address); @@ -233,9 +236,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command) { _status = _readCommand(RELAY_THREE_STATUS); if (_status == QUAD_RELAY_ON) - { return; - } else { _i2cPort->beginTransmission(_address); @@ -248,9 +249,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command) { _status = _readCommand(RELAY_FOUR_STATUS); if (_status == QUAD_RELAY_ON) - { return; - } else { _i2cPort->beginTransmission(_address); @@ -289,9 +288,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command) { _status = _readCommand(RELAY_ONE_STATUS); if (_status == QUAD_RELAY_OFF) - { // Is the board off? return; // Do nothing... - } else { // Then it must be on... _i2cPort->beginTransmission(_address); // Start communication. @@ -304,9 +301,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command) { _status = _readCommand(RELAY_TWO_STATUS); if (_status == QUAD_RELAY_OFF) - { return; - } else { _i2cPort->beginTransmission(_address); // Start communication. @@ -319,9 +314,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command) { _status = _readCommand(RELAY_THREE_STATUS); if (_status == QUAD_RELAY_OFF) - { return; - } else { _i2cPort->beginTransmission(_address); // Start communication. @@ -334,9 +327,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command) { _status = _readCommand(RELAY_FOUR_STATUS); if (_status == QUAD_RELAY_OFF) - { return; - } else { _i2cPort->beginTransmission(_address); // Start communication. @@ -362,7 +353,7 @@ uint8_t Qwiic_Relay::_readCommand(uint8_t _command) _i2cPort->write(_command); _i2cPort->endTransmission(); - _i2cPort->requestFrom(_address, 1); + _i2cPort->requestFrom(_address, (uint8_t)1); uint8_t status = _i2cPort->read(); return (status); } @@ -374,7 +365,7 @@ float Qwiic_Relay::_readVersion(uint8_t _command) _i2cPort->write(_command); _i2cPort->endTransmission(); - _i2cPort->requestFrom(_address, 2); + _i2cPort->requestFrom(_address, (uint8_t)2); float _versValue = _i2cPort->read(); _versValue += (float)_i2cPort->read() / 10.0; return (_versValue); diff --git a/src/SparkFun_Qwiic_Relay.h b/src/SparkFun_Qwiic_Relay.h index 7377325..628264b 100644 --- a/src/SparkFun_Qwiic_Relay.h +++ b/src/SparkFun_Qwiic_Relay.h @@ -1,5 +1,4 @@ -#ifndef _SPARKFUN_QWIIC_RELAY_H_ -#define _SPARKFUN_QWIIC_RELAY_H_ +#pragma once #include #include @@ -46,7 +45,7 @@ enum SF_SINGLE_RELAY_COMMANDS TURN_RELAY_OFF = 0x00, TURN_RELAY_ON, - FIRMWARE_VERSION = 0x04, + SFE_SINGLE_FIRMWARE_VERSION = 0x04, MYSTATUS }; @@ -67,7 +66,8 @@ enum SF_SINGLE_RELAY_STATUS #define DUAL_SSR_DEFAULT_ADDRESS 0x0A #define DUAL_SSR_ALTERNATE_ADDRESS 0x0B -#define ADDRESS_LOCATION 0xC7 +#define QUAD_CHANGE_ADDRESS 0xC7 +#define SINGLE_CHANGE_ADDRESS 0x03 #define INCORR_PARAM 0xFF class Qwiic_Relay @@ -136,7 +136,7 @@ class Qwiic_Relay // This function changes the I-squared-C address of the Qwiic RFID. The address // is written to the memory location in EEPROM that determines its address. - bool changeAddress(uint8_t newAddress); + bool changeAddress(uint8_t newAddress, bool singleRelay = false); private: uint8_t _address; @@ -169,4 +169,3 @@ class Qwiic_Relay TwoWire *_i2cPort; }; -#endif