I2C protocol Programming

Thread Starter

Tinashe228

Joined May 2, 2021
17
Activity 3: Serial communication protocols
2. Build the electronic shown on page 2;
3. Considering the Arduino connected to the LED as the master and that the one connected to the push button as the slave, write 2 text codes (one on the master side and one on the slave side) that enables the master and the slave to communicate using the I2C protocol. Please see below the communication steps that need to be implemented using the I2C protocol:
1. Master requests the status of digital pin 4 (S_Pin4) of the slave device;
2. Slave reads the status of its digital pin 4 (S_Pin4), displays it on the serial monitor and communicates it to the Master;
3. The master compares the status of S_Pin4 of the slave to “0” and implements the logic below:
If S_Pin4 >0; LED is switched “On”;
If S_Pin4 <0; LED is switched “Off”;


1619996558816.png
 

KeithWalker

Joined Jul 10, 2017
1,919
This looks very much like a college project. We do not write software for you.How do you propose to tackle it? We will try to help if you run into problems that you can not solve.
 

ericgibbs

Joined Jan 29, 2010
13,665
hi T,
No problem, post your code and any info that could help us debug your project.;)
E

The screen shot is difficult to read
 

Thread Starter

Tinashe228

Joined May 2, 2021
17
C-like:
#include <Wire.h>
int pushButton = A0;
int x = 0;
void setup()
{
  Wire.begin();
  pinMode(pushButoon, INPUT);
} 

void loop()
{
  Wire.beginTransmission(1);
  x = digitalRead(pushButton);
  Wire.write(x);
  Wire.endTransmission();
  delay(500);
}
 
Last edited by a moderator:

Thread Starter

Tinashe228

Joined May 2, 2021
17
For the slave:
#include <Wire.h>

int pushButton = 4;

int x = 0;

void setup()

{

  Wire.begin();

  pinMode(pushButoon, INPUT);

}


void loop()

{

  Wire.beginTransmission(1);

  x = digitalread(pushButton);

  Wire.write(x);

  Wire.endTransmission();

  delay(500);

}

FOR THE SLAVE
 
Last edited by a moderator:

Thread Starter

Tinashe228

Joined May 2, 2021
17
C-like:
#include <Wire.h>
int pinLed=8:
int x =0;

void setup()
{
  Wire.begin(1);
  Wire.onReceive(receiveEvent);
  pinMode(pinLed, OUTPUT);
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany){
 
x = Wire.read();
 
  if (x == 1){
   
        digitalWrite(pinLed,HIGH):
  }
  else{
        digitalWrite(pinLed,LOW);
  }
}
Mod edit - code tags - JohnInTX
 
Last edited by a moderator:

ericgibbs

Joined Jan 29, 2010
13,665
hi T,
Please don't keep flooding the thread with posts, lets do this one step at a time.:)
E

Lets focus on getting the Master code to compile. OK


BTW: The Slave has the same spelling error.
pinMode(pushButoon, INPUT);
 

Thread Starter

Tinashe228

Joined May 2, 2021
17
so this is the main error i get i do not know were to put the coma or the 5:13: the X was not declared and i dnt understand the 26:34
 

ericgibbs

Joined Jan 29, 2010
13,665
hi T,
The Slave code.
x = digitalRead(pushButton);

The commands are Case sensitive.

Then it compiles OK.

Now you do some tests.

E
OT: ZA is my second home in PE
 

Thread Starter

Tinashe228

Joined May 2, 2021
17
hi T,
The Slave code.
x = digitalRead(pushButton);

The commands are Case sensitive.

Then it compiles OK.

Now you do some tests.

E
OT: ZA is my second home in PE
So the digital read is fine is fixed the R is a capital letter the cases are fine its the commas and that x was not declared on the scope that im not sure of... Oh that awesome glad to have a fellow South African helping out
 

ericgibbs

Joined Jan 29, 2010
13,665
hi T,
Your Master Sketch, added two lines for testing with the Serial Monitor.
print 0 when A0 is Low and 1 when A0 is High.
E
C++:
// T Master 03/05/21 EG57A

#include <Wire.h>
int pushButton = A0;
int x = 0;
void setup()
{
Wire.begin();
pinMode(pushButton, INPUT);
Serial.begin(9600);// for testing program ONLY

}

void loop()
{
Wire.beginTransmission(1);
x = digitalRead(pushButton);
Wire.write(x);
Serial.println(x);// for testing program ONLY

Wire.endTransmission();
delay(500);
}
 

Attachments

ericgibbs

Joined Jan 29, 2010
13,665
hi T,
The Slave with the two lines added.
It works, the rest is up to you, if you get stuck ask.
E
C++:
// T_Slave 03/05/21 EG57A

#include <Wire.h>
int pushButton = 4;
int x = 0;


void setup()
{
Wire.begin();
pinMode(pushButton, INPUT);
Serial.begin(9600);// for testing program ONLY
}

void loop()
{
Wire.beginTransmission(1);
x = digitalRead(pushButton);
Wire.write(x);
Serial.println(x);// for testing program ONLY

Wire.endTransmission();
delay(500);
}
 

Attachments

Top