Retested again. The DigiKeyboard sample still works fine and without the pull-up resistors I get the incorrect constant range printing out (like before). I also added in some additional print statements when testing with the pull-ups and I only get some of my print statements (and no range) but I'm not sure what might be causing the issue. I'm including my code and I keep getting the following printout though the 0 does not always show up. Any ideas what might be going on here? I'm looking at TinyWireM.endTransmission(); because I see no print statement after.
0
Test
loc:1
loc:2
loc:3
0
Test
loc:1
loc:2
loc:3
/* Code for Arduino Uno R3 modified for Digispark
Assumes the sensor is using the default address
Sensor Connections:
Pin 7 to GND
Pin 6 to 5V
Pin 5 to SCL
Pin 4 to SDA
Requires pull-ups for SCL and SDA connected to 5V to work reliably
*/
#include "DigiKeyboard.h" //included in order to debug
#include "TinyWireM.h"
//The Arduino Wire library uses the 7-bit version of the address, so the code example uses 0x70 instead of the 8-bit 0xE0
//I don't know if this statement holds true for TinyWireM
#define SensorAddress byte(0x70)
//The sensors ranging command has a value of 0x51
#define RangeCommand byte(0x51)
//These are the two commands that need to be sent in sequence to change the sensor address
#define ChangeAddressCommand1 byte(0xAA)
#define ChangeAddressCommand2 byte(0xA5)
void setup() {
Serial.begin(9600); //Open serial connection at 9600 baud
TinyWireM.begin(); //Initiate TinyWireM library for I2C communications with the I2CXL-MaxSonar-EZ
}
void loop(){
DigiKeyboard.println(0);
DigiKeyboard.println("Test");
takeRangeReading(); //Tell the sensor to perform a ranging cycle
DigiKeyboard.println("got here");
DigiKeyboard.delay(100); //Wait for sensor to finish
word range = requestRange(); //Get the range from the sensor
DigiKeyboard.println("Range: ");
DigiKeyboard.println(range); //Print to the user
}
//Commands the sensor to take a range reading
void takeRangeReading(){
DigiKeyboard.println("loc:1");
TinyWireM.beginTransmission(SensorAddress); //Start addressing
DigiKeyboard.println("loc:2");
TinyWireM.send(RangeCommand); //send range command
DigiKeyboard.println("loc:3");
TinyWireM.endTransmission(); //Stop and do something else now
}
//Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no communication.
word requestRange(){
DigiKeyboard.println("loc:4");
TinyWireM.requestFrom(SensorAddress, byte(2));
if(TinyWireM.available() >= 2){ //Sensor responded with the two bytes
byte HighByte = TinyWireM.receive(); //Read the high byte back
byte LowByte = TinyWireM.receive(); //Read the low byte back
word range = word(HighByte, LowByte); //Make a 16-bit word out of the two bytes for the range
return range;
}
else {
return word(0); //Else nothing was received, return 0
}
}
/* Commands a sensor at oldAddress to change its address to newAddress
oldAddress must be the 7-bit form of the address that is used by Wire
7BitHuh determines whether newAddress is given as the new 7 bit version or the 8 bit version of the address
If true, if is the 7 bit version, if false, it is the 8 bit version
*/
void changeAddress(byte oldAddress, byte newAddress, boolean SevenBitHuh){
DigiKeyboard.println("loc:5");
TinyWireM.beginTransmission(oldAddress); //Begin addressing
TinyWireM.send(ChangeAddressCommand1); //Send first change address command
TinyWireM.send(ChangeAddressCommand2); //Send second change address command
byte temp;
if(SevenBitHuh){ temp = newAddress << 1; } //The new address must be written to the sensor
else { temp = newAddress; } //in the 8bit form, so this handles automatic shifting
TinyWireM.send(temp); //Send the new address to change to
TinyWireM.endTransmission();
}