Skip to content

Commit c7c60e4

Browse files
committed
Example 3 - Change I2C address working
1 parent ed074d2 commit c7c60e4

File tree

1 file changed

+81
-64
lines changed

1 file changed

+81
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,101 @@
11
/*
2-
An I2C based KeyPad
3-
By: Nathan Seidle
4-
SparkFun Electronics
5-
Date: January 21st, 2018
6-
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
2+
This is a example written for the SparkFun Qwiic Keypad
3+
SparkFun sells these at its website: www.sparkfun.com
4+
Do you like this library? Help support SparkFun. Buy a board!
5+
https://www.sparkfun.com/products/15168
76
8-
Feel like supporting our work? Buy a board from SparkFun!
9-
https://www.sparkfun.com/products/14641
7+
Originally written by Wes Furuya @ SparkFun Electronics, February 5th, 2019
8+
For the Qwiic Joystick.
9+
Modified for Keypad by Pete Lewis @ SparkFun Electronics, March 17th, 2019
1010
11-
This example demonstrates how to change the I2C address via software.
11+
The Qwiic Keypad is an I2C controlled ATTiny84-based 12 Button keypad.
12+
13+
Example 3 - Change I2C Address and Read Firmware Version:
14+
This program uses the Qwiic Keypad Arduino Library to change the I2C address
15+
for the device. You enter in the DEC value (0-127) of the I2C address
16+
and wait 1 second.
17+
18+
https://github.com/sparkfun/SparkFun_Qwiic_Keypad_Arduino_Library/examples
1219
13-
Note: To change the address you can also open the on-board jumper. This will force the address to 74 (0x4A).
20+
Development environment specifics:
21+
Arduino IDE 1.8.8
1422
15-
Note: If you change the address to something unknown you can either open the jumper (goes to address 0x4A) or
16-
use the I2C scanner sketch (Example 4).
23+
Qwiic KeyPad records any button presses to a stack. It can remember up to 15 buttone presses.
24+
The master I2C device (for example, an Uno) can ask for the oldest button pressed.
25+
If the master continues to read in button presses, it will receive the entire stack (from oldest to newest).
26+
This is handy if you need to go and do something else with your code, you can then come back to the
27+
keypad and pull in the last 15 button presses.
28+
29+
This program is distributed in the hope that it will be useful,
30+
but WITHOUT ANY WARRANTY; without even the implied warranty of
31+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32+
GNU General Public License for more details.
33+
34+
You should have received a copy of the GNU General Public License
35+
along with this program. If not, see <http://www.gnu.org/licenses/>.
1736
*/
1837

1938
#include <Wire.h>
39+
#include "SparkFun_Qwiic_Keypad_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_keypad
40+
KEYPAD keypad1; //Create instance of this object
2041

21-
byte keypadAddress = 75; //75 is default, 74 if jumper is closed
22-
byte newAddress = 55; //Must be 0x08 <= newAddress <= 0x77
42+
uint8_t Address = 0x4B; //Start address (Default 0x4B)
43+
byte newAddress[5]; // used to store incoming new address from user via serial terminal
2344

24-
void setup(void)
25-
{
26-
Wire.begin();
27-
45+
void setup() {
2846
Serial.begin(9600);
29-
Serial.println("Qwiic KeyPad Change Address Example");
30-
Serial.println("Press a key to begin");
47+
Serial.println("Qwiic Keypad Example 3 - Change I2C Address");
3148

32-
while(Serial.available()) Serial.read(); //Clear buffer
33-
while(!Serial.available()) delay(1); //Wait for user to press button
34-
35-
//The new address must be 0x08 <= address <= 0x77
36-
if(changeKeyPadAddress(keypadAddress, newAddress) == true) //Old address, new address
49+
if(keypad1.begin(Wire, Address) == false)
3750
{
38-
keypadAddress = newAddress;
39-
Serial.println("Address successfully changed to 0x" + String(keypadAddress, HEX));
51+
Serial.println("Keypad does not appear to be connected. Please check wiring. Freezing...");
52+
while(1);
4053
}
41-
}
42-
43-
void loop(void)
44-
{
45-
char button = readKeyPad();
46-
47-
if(button == -1)
54+
else
4855
{
49-
Serial.println("No keypad detected");
50-
delay(1000);
56+
Serial.print("Address: 0x");
57+
Serial.print(Address, HEX);
58+
Serial.print(" Version: ");
59+
Serial.println(keypad1.getVersion());
5160
}
52-
else if(button != 0)
53-
{
54-
if(button == '#') Serial.println();
55-
else if(button == '*') Serial.print(" ");
56-
else Serial.print(button);
57-
}
58-
59-
//Do something else. Don't call readKeyPad a ton otherwise you'll tie up the I2C bus
60-
delay(25); //25 is good, more is better
6161
}
6262

63-
//Get the latest button
64-
char readKeyPad()
65-
{
66-
Wire.requestFrom((uint8_t)keypadAddress, (uint8_t)1);
67-
return(Wire.read());
68-
}
63+
void loop() {
64+
//Serial.println("I2C Range: 0-127");
65+
Serial.println("INPUT- New I2C Address (DEC):");
66+
Serial.flush(); //Clears buffer
67+
while (Serial.available() == 0) delay(20); //Waits for entry
68+
int len = Serial.readBytes(newAddress,5); //Takes entry as a stream of bytes
69+
70+
//Serial.println(len);
71+
//Serial.println(newAddress[0]);
72+
//Serial.println(newAddress[1]);
73+
//Serial.println(newAddress[2]);
6974

70-
//Change the I2C address from one address to another
71-
boolean changeKeyPadAddress(byte oldAddress, byte newAddress)
72-
{
73-
Wire.beginTransmission(oldAddress); //Communicate using the old address
74-
Wire.write(0xC7); //0xC7 is the register location on the KeyPad to change its I2C address
75-
Wire.write(newAddress); //Go to the new address
76-
if (Wire.endTransmission() != 0)
77-
{
78-
//Sensor did not ACK
79-
Serial.println("Error: Sensor did not ack");
80-
return(false);
81-
}
82-
return(true);
83-
}
75+
//Converts ASCII char to DEC (Address)
76+
if (len==1)
77+
{
78+
// Serial.println((byte)newAddress[0]-48, HEX);
79+
Address = (byte)newAddress[0]-48;
80+
}
81+
else if (len==2)
82+
{
83+
// Serial.println(((byte)newAddress[0]-48)*10+((byte)newAddress[1]-48), HEX);
84+
Address = ((byte)newAddress[0]-48)*10+((byte)newAddress[1]-48);
85+
}
86+
else if (len==3)
87+
{
88+
// Serial.println(((byte)newAddress[0]-48)*100+((byte)newAddress[1]-48)*10+((byte)newAddress[2]-48), HEX);
89+
Address = ((byte)newAddress[0]-48)*100+((byte)newAddress[1]-48)*10+((byte)newAddress[2]-48);
90+
}
91+
92+
Serial.print("new address will be: ");
93+
Serial.println(Address, HEX);
94+
95+
keypad1.setI2CAddress(Address); //Sets new I2C address
96+
97+
//Print out Firmware Version to double check address change
98+
Serial.print(" Firmware: ");
99+
Serial.println(keypad1.getVersion());
84100

101+
}

0 commit comments

Comments
 (0)