0

Im trying to send data from slave (writer) to master (reader) then compare it to declared 'char p[]' to turn on the lights but somehow i cant make it work.

////master reader
 #include <Wire.h>
int LED = 13;
char p[] = "YES..."; 
void setup() {
 pinMode(LED, OUTPUT);
 Wire.begin(); // join i2c bus (address optional for master)
 Serial.begin(9600); // start serial for output
}
void loop() {
 Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
 while (Wire.available()) { // slave may send less than requested
 char c= Wire.read(); // receive a byte as character
 Serial.print(c); // print the character
 if(c == p){
 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level) 
 }else{ 
 digitalWrite(LED, LOW); // turn the LED on (HIGH is the voltage level) 
 }
 }
 delay(1500);

Slave

////////////// Slave ///////////////////////
 #include <Wire.h>
const int pingPin = 8; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 9; // Echo Pin of Ultrasonic Sensor
void setup() {
 Serial.begin(9600); // Starting Serial Terminal
 Wire.begin(8); // join i2c bus with address #8
}
void loop() {
 long duration, inches, cm;
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(pingPin, LOW);
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);
 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 delay(1500);
if( inches <= 4){
 Wire.onRequest(requestEvent); // register event
 Serial.print("Obstacle detected");
 Serial.println();
 }
 else
 {
 Wire.onRequest(requestEvent1); // register event
 Serial.print("Nothing Detected");
 Serial.println();
 }
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
 Wire.write("YES..."); // respond with message of 6 bytes
 // as expected by master
}
void requestEvent1() {
 Wire.write("NO...."); // respond with message of 6 bytes
 // as expected by master
}
long microsecondsToInches(long microseconds) {
 return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
 return microseconds / 29 / 2;
}
Jot
3,2761 gold badge14 silver badges21 bronze badges
asked Apr 22, 2019 at 10:54

1 Answer 1

1
  1. Improve you indentation.
  2. If you want to compare two strings use strcmp function for that.
  3. Your master part has some brackets left in your void loop() function, correct them.
  4. In I2C you are receiving one byte at time. You should receive data till the last byte in received.
  5. You should have some identification in your message, which is you are sending from slave to master indicating that this is last character. It helps you come out of Data reception loop in Master side.
answered Apr 22, 2019 at 11:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.