Hello!
It's about this situation:
I have 2 (in the future more than 2) Atmega328. One has the main program; the other I want to use it to colect data (digital/analogical) and to send the information to the main chip; there are more than 8 parameters to send.
Than, after the master chip precesses the information, it will send to the slave an order to drive up some pins.
And so on...
I've searched for information that could help me, but I didn't find anything cogent.
What I don't know is how to send/receive more than one variable. I need to send to the master the state of some digital pins and the values from some analogical pins; the master will do some calculations and then will sent to the slave what pins needs to be HIGH/LOW or some analogical out... depending the results from the calculations.
So, the starting point needs to be the point where you send/receive the second variable, isn't it?
As I see, it matters the order of the sent data.
If the slave will assign the values received to "a" and to "b" and the master sends first the state of the pin 5 and then the state of the pin 6, the result will be like this: a=pinState5, b=pinState6. If the master will send first the state of the pin 6, the result will be this: a=pinState6, b=pinState5. Am I understood right?
Well Hmm...
Lets say you have a Value read on the master Unit Example -Voltage in- . THe Master unit take that Value asigns it a Varable (vin = 5)
Take that Value the sends it VIA I2C (Wire.write(vin) To a given address ( address x) . Then you would have Slave (address x) recieve that data (vin = Wire.read(); ) . And at that point you could do anything you want. Example
of course there will be other code just like any other Sketch , but that will be the basics,ow and order of data sent isnt important,its all send at one time
#include <Wire.h> //starts the library
#define LED_2 11
int x = 0; // give a value to the varable to be change with I/Os
int y = 0;
void setup() {
// Serial.begin(9600); // used to debug
Wire.begin(2); // Start I2C Bus as a Slave (Device Number 9)
Wire.onReceive(receiveEvent); // register event
pinMode(LED_2, OUTPUT);
digitalWrite(LED_2, LOW);
}
void loop ()
{
if (x == 1) {
digitalWrite(LED_2, HIGH);
delay(200);
digitalWrite(LED_2, LOW);
delay(200);
}
}
void receiveEvent(int howMany)
{
x = Wire.read(); // receive byte as an integer
y = Wire.read(); // receive byte as an integer
}
Master
#include <Wire.h> //starts the library
int INRead = A0;
int x = 0; // give a value to the varable to be change with I/Os
int y = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(INRead, INPUT);
Wire.begin(1);
}
void loop(){
{
x = analogRead(INRead);
}
Wire.beginTransmission(2);
Wire.write(x);
Wire.endTransmission();
}
I just wrote this - so take it for whats its worth
There is alot left out ,, youll have to add your own code from there
My problem is when I want to read FROM a slave (the slave sends data to the master) MORE than 1 variable!!!! I don't know how to build the code to read all the variables is sending the slave. With 1 variable, works; when I send more than 1, it (the master) reads just the last one. How do I solve this??
But this code is for Master sender
This is not my problem! My problem is for MASTER READER / SLAVE SENDER with more variables. With one, yes it works.
Anyway, thank you very much for your interest in my problem.
So, anyone knows how to send more than 1 variable from a slave (slave - sender) to a master (master - reader) via I2C???