@Mars-Sojourner, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that you write ( ), not for questions.
Please provide details; we don't know what you've found.
// Arduino MASTER sketch
#include <Wire.h>
byte i2c_rcv; // data received from I2C bus
unsigned long time_start; // start time in milliseconds
int stat_LED; // status of LED: 1 = ON, 0 = OFF
byte value_pot; // potentiometer position
void setup() {
Wire.begin(); // join I2C bus as the master
// initialize global variables
i2c_rcv = 255;
time_start = millis();
stat_LED = 0;
pinMode(13, OUTPUT); // set pin 13 as an output
}
void loop() {
// read potentiometer position
value_pot = analogRead(A0); // read the voltage at pin A0 (potentiometer voltage)
// send potentiometer position to Slave device 0x08
Wire.beginTransmission(0x08); // informs the bus that we will be sending data to slave device 8 (0x08)
Wire.write(value_pot); // send value_pot
Wire.endTransmission(); // informs the bus and the slave device that we have finished sending data
Wire.requestFrom(0x08, 1); // request potentiometer position from slave 0x08
if (Wire.available()) { // read response from slave 0x08
i2c_rcv = Wire.read();
}
// blink logic code
if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
stat_LED = !stat_LED;
time_start = millis();
}
digitalWrite(13, stat_LED);
}
// Arduino SLAVE sketch
#include <Wire.h>
byte i2c_rcv; // data received from I2C bus
unsigned long time_start; // start time in mSec
int stat_LED; // status of LED: 1 = ON, 0 = OFF
byte value_pot; // potentiometer position
void setup() {
Wire.begin(0x08); // join I2C bus as Slave with address 0x08
// event handler initializations
Wire.onReceive(dataRcv); // register an event handler for received data
Wire.onRequest(dataRqst); // register an event handler for data requests
// initialize global variables
i2c_rcv = 255;
time_start = millis();
stat_LED = 0;
pinMode(13, OUTPUT); // set pin 13 mode to output
}
void loop() {
value_pot = analogRead(A0); // read analog value at pin A0 (potentiometer voltage)
// blink logic code
if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
stat_LED = !stat_LED;
time_start = millis();
}
digitalWrite(13, stat_LED);
}
//received data handler function
void dataRcv(int numBytes) {
while (Wire.available()) { // read all bytes received
i2c_rcv = Wire.read();
}
}
// requests data handler function
void dataRqst() {
Wire.write(value_pot); // send potentiometer position
}
Thank you for the reply. Please find the code in the reply above. As i said, I want to blink an LED on the slave.
I dont know if I should insert the LED Blink code in the slave or master.
Let us say that the LED blink code needs to be on the slave, I dont know which line the LED blink code goes? Do I just paste it at the end?
Below is the LED blink code:
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
The slave sketch performs a Wire.write, a send back of data.
Add a line making a digitalWrite to the led pin.
The slave code echoes data back to the master but that's an option that can be used later I suppose.
Ty. So I want to blink and LED connected to PIN 12.
So would the new slave code look like the following? (My PIN 12 code is on bold )
// Arduino slave sketch
#include <Wire.h>
byte i2c_rcv; // data received from I2C bus
unsigned long time_start; // start time in mSec
int stat_LED; // status of LED: 1 = ON, 0 = OFF
byte value_pot; // potentiometer position
//...................................................................................
// My LED Blink code that I inserted begins here.
//
//
void setup() {
// initialize digital pin 12 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
//
//
// My LED Blink code that I inserted ends here.
//...............................................................................
void setup() {
Wire.begin(0x08); // join I2C bus as Slave with address 0x08
// event handler initializations
Wire.onReceive(dataRcv); // register an event handler for received data
Wire.onRequest(dataRqst); // register an event handler for data requests
// initialize global variables
i2c_rcv = 255;
time_start = millis();
stat_LED = 0;
pinMode(13, OUTPUT); // set pin 13 mode to output
}
void loop() {
value_pot = analogRead(A0); // read analog value at pin A0 (potentiometer voltage)
// blink logic code
if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
stat_LED = !stat_LED;
time_start = millis();
}
digitalWrite(13, stat_LED);
}
//received data handler function
void dataRcv(int numBytes) {
while (Wire.available()) { // read all bytes received
i2c_rcv = Wire.read();
}
}
// requests data handler function
void dataRqst() {
Wire.write(value_pot); // send potentiometer position
}
Then the Master sends a command "start blinking led" or "stop blinking led", and the Slave does the blinking ?
The sketch will be easier if you send a single byte over the I2C bus.
For example a single byte: 100 is start blinking and 101 is stop blinking.
The Slave needs to check a global variable and the global variable is set in the onReceive handler.
Ok Ty. I removed the loop and made it blink the hard way for now.
When the master sends a command I want the slave to Blink the LED on the slave's pin 12.
Ty
Please compile the code and make a test run. No offence but my feeling is that You're a bit new in coding. Evaluating untested desk produced code is a waist of time.
Now You show 2 instances of Setup. That's no, no.
You have a noname function starting as "void { digitalWrite(12...."
Compile and correct.
Oh Wow! thanks a lor for the reply!!!
Let me digest that and will get back to you if I got questions.
(Will give St.Jude's Children's hospital $1 donation for your help)
Ty