Hi guys,
I'm really new to use I2C to send data to a display. I have a sparkfun 7 segments display. I can run it perfect with the serial port. However, I need use serial port to send data through Bluetooth. Then, I found it can also run as a I2C device. It works ok until I found weird digit jumping.
The 0 should be shown on the right side instead of left.
Incorrect:
Correct:
This always happened when I plug in another arduino as I2C master board. When I do not plug the master board, It also happened twice. And, it will display normal when I unplug the power to reboot (tried reboot button, it will not change the digit location).
can someone help me on this problem? Thank you very much.
/*
BBQube Slave 0.0.1
receive data from master unit
execute the code to control blower
05/04/2016
A5 to SCL
A4 to SDA
VIN to PWR
GND to GND
last version can not bootup reprogram from start..
*/
//define LED Display
#include <Wire.h>
#define DISPLAY_ADDRESS1 0x71
//define arduino address
#define slave_address 8
//define Buttons
#include<Button.h>
#define PULLUP true
#define INVERT false
#define DEBOUNCE_MS 20
#define LONG_PRESS 1000
Button Button_play(4, PULLUP, true, DEBOUNCE_MS);
Button Button_set(7, PULLUP, INVERT, DEBOUNCE_MS);
#include<RotaryEncoder.h>
RotaryEncoder encoder(5, 6);
static int pos = 0;
#define Relay 8
#define FanPin 9
#define FanControl 10
#define led1 2
#define led2 3
int buttoncase, state, display_state;
int target_temp, target_input, target_temp_display;
int button_count, display_count;
int fan_power, temp_read, ES_upper, ES_lower;
int signal_slave, target_temp_hear, state_hear, P1_temp, signal_hear;
int Button_play_count;
int Inputread;
char tempString[10];
int Data_input [4] = {0, 0, 0, 0};
int Data_output [3] = {0, 0, 0};
unsigned long current_time, old_time, old_time_display;
uint8_t Buffer[3];
void setup() {
pinMode(FanPin, OUTPUT);
pinMode(FanControl, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(Relay, HIGH);
signal_slave = 0;
target_temp_hear = 0;
state_hear = 0;
P1_temp = 0;
signal_hear = 0;
buttoncase = 0;
button_count = 0;
state = 0;
display_state = 0;
target_temp_display = 0;
target_temp = 0;
target_input = 200;
temp_read = 0;
fan_power = 0;
ES_upper = 0;
ES_lower = 0;
Button_play_count = 0;
Wire.begin(slave_address);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Wire.endTransmission();
//Send the reset command to the display - this forces the cursor to return to the beginning of the display
Wire.beginTransmission(DISPLAY_ADDRESS1);
s7sSendStringI2C("----");
delay(1000);
Wire.endTransmission(DISPLAY_ADDRESS1);
Serial.begin(9600);
}
void loop() {
current_time = millis();//start count time as millisecond
case_select();
encoderread();
control();
led_control();
i2c_com();
LEDSendValue ();
}
void LEDSendValue () {
if (display_state == 0) {
target_temp_display = target_temp;
sprintf(tempString, "%4d", target_temp_display);
s7sSendStringI2C(tempString);
delay(50);
}
if (display_state == 1) {
target_temp_display = target_input;
if (current_time - old_time_display > 300) {
display_count = display_count + 1;
if ( (display_count % 2) == 0) {
sprintf(tempString, "%4d", target_temp_display);
s7sSendStringI2C(tempString);
delay(50);
}
else {
s7sSendStringI2C(" ");
}
old_time_display = current_time;
}
}
}
void clearDisplayI2C()
{
Wire.beginTransmission(DISPLAY_ADDRESS1);
Wire.write(0x76); // Clear display command
Wire.endTransmission(DISPLAY_ADDRESS1);
}
void s7sSendStringI2C(String toSend)
{
Wire.beginTransmission(DISPLAY_ADDRESS1);
for (int i = 0; i < 4; i++)
{
Wire.write(toSend[i]);
}
Wire.endTransmission(DISPLAY_ADDRESS1);
}
void encoderread() {
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
//Serial.println(newPos);
pos = newPos;
encoder.setPosition(0);
} // if
} // detect the encoder rotate
void case_select() {
Button_play.read();
Button_set.read();
encoderread();
switch (buttoncase) {
case 0:
if (Button_set.wasReleased()) {
buttoncase = 1;
button_count = 1;
Serial.println("triger");
}
else if (Button_set.pressedFor(LONG_PRESS)) {
if (state == 2) {
buttoncase = 2;
button_count = 3;
}
else {
buttoncase = 3;
button_count = 4;
}
}
else if (Button_play.pressedFor(LONG_PRESS)) {
state = 0;
target_temp = 0;
signal_slave = 1;
//Serial.println("triger");
}
else if (Button_play.wasReleased()) {
Button_play_count = Button_play_count + 1;
if (Button_play_count > 3) {
state = 1;
Button_play_count = 0;
}
}
display_state = 0;
break;
case 1:
if (pos == -1) {
target_input = target_input - 5;
button_count = 5;
}
else if (pos == 1) {
target_input = target_input + 5;
button_count = -5;
}
else if (Button_play.wasReleased()) {
buttoncase = 0;
target_temp = target_input;
button_count = 0;
signal_slave = 1;
}
display_state = 1;
//check_connect = 1;
break;
case 2:
state = 0;
if (Button_set.wasReleased()) {
buttoncase = 0;
}
break;
case 3:
state = 2;
if (Button_set.wasReleased()) {
buttoncase = 0;
}
break;
}
//Serial.print(buttoncase);
//Serial.print(":::");
//Serial.println(button_count);
}
void control() {
if (current_time - old_time > 2000) {
if (state == 0) {
fan_power = 0;
analogWrite(FanPin, fan_power);
}
else if (state = 1) {
fan_power = 255;
analogWrite(FanPin, fan_power);
}
else if (state == 2) {
ES_upper = target_temp + 3;
ES_lower = target_temp - 3;
if (temp_read <= ES_lower) {
digitalWrite(Relay, LOW);
}
else if (temp_read >= ES_upper) {
digitalWrite(Relay, HIGH);
}
}
else if (state == 3) {
}
Serial.print(Buffer[0]);
Serial.print(":::");
Serial.print(Buffer[1]);
Serial.print(":::");
Serial.println(Buffer[2]);
old_time=current_time;
}
}
void i2c_com() {
state_hear = Data_input[0];
target_temp_hear = Data_input[1];
P1_temp = Data_input[2];
signal_hear = Data_input[3];
if (signal_hear == 1) {
state = state_hear;
target_temp = target_temp_hear;
signal_slave = 2;
}
if (signal_hear == 2) {
signal_slave = 0;
}
}
void led_control () {
if (state == 0) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
else if (state == 2) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
}
}
void receiveEvent(int howMany) {
for (int i=0;i<4;i++) {
Data_input[i] = Wire.read();
}
Serial.print(Data_input[0]);
Serial.print(":::");
Serial.print(Data_input[1]);
Serial.print(":::");
Serial.print(Data_input[2]);
Serial.print(":::");
Serial.println(Data_input[3]);
}
void requestEvent() {
Data_output[0] = state;
Data_output[1] = target_temp;
Data_output[2] = signal_slave;
Buffer[0]=Data_output[0];
Buffer[1]=Data_output[1];
Buffer[2]=Data_output[2];
Wire.write(Buffer,6);
}

