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;
}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.