and yes that did compile
Really? You have a variable called po1char AND one called potchar1? Idiot. There are plenty of names to choose from. Don't use names that are rearrangements like that.
and yes that did compile
Really? You have a variable called po1char AND one called potchar1? Idiot. There are plenty of names to choose from. Don't use names that are rearrangements like that.
PaulS:
Really? You have a variable called po1char AND one called potchar1? Idiot. There are plenty of names to choose from. Don't use names that are rearrangements like that.
Can you please refrain from insulting people. There are better ways to tell somebody that thir approach is wrong / dangerous / whatever.
Can you please refrain from insulting people.
I would, except that clearly that code did NOT compile.
PaulS:
Really? You have a variable called po1char AND one called potchar1? Idiot. There are plenty of names to choose from. Don't use names that are rearrangements like that.
I don't appreciate being spoken to in such way. If you're thinking I'm an IDIOT please leave. I have seen your comments on other topics and the way you talk to people is wrong. Your behaviour is not acceptable...
I'm constantly making commits to my code, to make it work. IF I EVER MAKE SUCH MISTAKE I will find it out soon or late. As I said thy're just names and won't matter if there is a number after it in between or etc. etc.....
Cool it please.
Cut the ad hominems, please.
As I said thy're just names and won't matter if there is a number after it in between
No, they won't directly hurt anything/anyone, but sensible naming helps reduce hair-loss.
Back to topic:
I was trying to send multiple data over the bus and needed help packing and unpacking the messages.
I have already posted a method that I used for Serial Communication earlier. Was wondering if the same method would apply?? and would work??
Post your code as it is now, explain what it does do that you don't expect, and what it doesn't do that you do expect.
And please, cut out the multiple punctuation - it's really annoying.
Here is the master code:
it requests bytes/data/info from device #8 in the I2CBus.
It displays the data on a LCD as well as Serial buffer. Works fine beside the fact that the update rate it slow.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define LED_OFF 0
#define LED_ON 1
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
byte c;
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
lcd.begin (16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(LED_ON);
}
void loop() {
Wire.requestFrom(8, 1); // request 1 byte from slave device #8
while (Wire.available()) { // slave may send less than requested
c = Wire.read(); // receive a byte as character
// c = c + '0';
Serial.print(c); // print the character
}
Serial.println();
delay(500);
lcd.clear();
delay(1000);
lcd.home();
// Print our characters on the LCD
lcd.backlight(); //Backlight ON if under program control
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Pot 1 = ");
lcd.print(c);
lcd.print(" ohm");
}
Here is the Slave code (Device #8)
at the moment it reads values of a pot and up on request of the master it sends it.
Now i want to have the second and the third pot or sensor and pack the messages to be sent to master.
#include <Wire.h>
int pot1;
char pot1char;
void setup() {
Serial.begin(9600);
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
pot1 = analogRead(A0);
pot1 /= 4;
pot1char = char(pot1);
Serial.println(pot1);
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write(pot1char);// respond with message of 1 byte
// as expected by master
}
both codes work well at this time but i'm unsure of packing and unpacking the messages.
See the attached pic to see what's going on the I2cBus
delay(500);
The update rate is slow, you say?
if i comment out that delay(500); the LCD won't show anything. I fell like It won't have enough time to upload!?
amirrajabifar:
if i comment out that delay(500); the LCD won't show anything. I fell like It won't have enough time to upload!?
That usually means that you are continually updating the LCD, which disturbs the display. That, instead of only updating the display when something actually changes.
After some more coding
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define LED_OFF 0
#define LED_ON 1
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
byte c;
boolean lastmessage = LOW;
boolean currentmessage = LOW;
boolean changeState (boolean last){
boolean current = Wire.read();
if (last != current) {
current = Wire.read();
}
return current;
}
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
lcd.begin (16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(LED_ON);
}
void loop() {
Wire.requestFrom(8, 1); // request 1 byte from slave device #8
while (Wire.available()) { // slave may send less than requested
c = Wire.read(); // receive a byte as character
// c = c + '0';
Serial.print(c); // print the character
}
Serial.println();
delay(500);
currentmessage = changeState(lastmessage);
if (lastmessage == LOW && currentmessage == HIGH) {
lcd.clear();
delay(1000);
lcd.home();
// Print our characters on the LCD
lcd.backlight(); //Backlight ON if under program control
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Pot 1 = ");
lcd.print(c);
lcd.print(" ohm");
}
}
I've tried to compare the data that i'm getting by implementing booleans. When i commented that delay(500); out the LCD was getting updated so fast that nothing was visible. After adding the delay back in the LCD was blinking and showing readable data.
Am i doing something I'm not suppose to do?
Am i doing something I'm not suppose to do?
Yes.
boolean lastmessage = LOW;
boolean currentmessage = LOW;
booleans should be assigned true or false.
boolean changeState (boolean last){
boolean current = Wire.read();
if (last != current) {
current = Wire.read();
}
return current;
}
Are you sending two values? Sometimes, you read one. Sometimes you read two. Not a good idea at all.
When i commented that delay(500); out the LCD was getting updated so fast that nothing was visible.
If you're worried about flickering, why not just update the LCD every 500ms or something like that?
Also don't clear the LCD every time you write to it, just use the cursor positioning and judicious writing of blank fills along with the data to the fields you write to.
Don't try to debug the LCD and the I2C at the same time. Use debug methods to get one working first, then go after the other one.
@PaulS I'm sending only one value.
amirrajabifar:
@PaulS I'm sending only one value.
You read one value, and assign it to current. If the value in current is not the same as the value in last, you read again, and then return the garbage value.
I can't quite fathom what you are attempting to do with that function. I can't quite fathom why you are using booleans.
LCD problem solved!
I commented out both delay(500); and lcd.clear();
I'm doing that to compare the statues of the data i'm receiving from the bus so the lcd would be updated only if the data has changed
amirrajabifar:
I'm doing that to compare the statues of the data i'm receiving from the bus so the lcd would be updated only if the data has changed
I fail to see how that action could accomplish what you're saying.
I'm doing that to compare the statues of the data i'm receiving from the bus so the lcd would be updated only if the data has changed
Create a global (or static) variable to hold the last value written to the LCD. Each time you write to the LCD, update that variable.
Each time there is new data, compare the new data to the old data, to see if the LCD needs to be written to. If the data is different, write the new data and save as the old data.