code

Hi all,

I 'm biginner on arduino,I have this code I can't understand it ,what is meanning.?

void loop()
{
x = analogRead(0);
y = analogRead(1);
v = analogRead(2);
z = analogRead(3);

if (x > y && x > v && x > z){
w=x;
}
if (y > x && y > v && y > z){
w=y;
}
if (v > y && v > x && v > z){
w=v;
}
if (z > y && z > v && z > x){
w=z;
}
sensor_4 = w;
//Serial.println(sensor_4);

Wire.requestFrom(1, 2); // request 6 bytes from slave device #2
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_1 = word(buffer[1],buffer[0]);
}

}
Wire.requestFrom(2, 2);
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_2 = word(buffer[1],buffer[0]);
}
}

Wire.requestFrom(3, 2);
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_3 = word(buffer[1],buffer[0]);
}
}

if (sensor_1 > seuil_1 && CurrentSensor == 0 ){
CurrentSensor = 1;
}
if (sensor_2 > seuil_2 && CurrentSensor == 0 ){
CurrentSensor = 2;
}
if (sensor_3 > seuil_3 && CurrentSensor == 0 ){
CurrentSensor = 3;
}
if (sensor_4 > seuil_3 && CurrentSensor == 0 ){
CurrentSensor = 4;
}

plz i need guidance.
Thanks in adance.
Regards.

Hi, welcome to the forum.

We need also some guidance. What is it ?

A number of functions are used from the Arduino libraries. You can find the reference online.

analogRead() reads an analog input pin.

Everything with "Wire" is the I2C bus. Read the reference online and have a look at some examples. There is a specific way to use those functions.

I think three Arduino boards are connected as I2C Slaves, and this sketch requests data from those Slaves.

it reads the inputs from 4 input pins (0,1,2 and 3)
it sets the variable w to hold the largest of these four readings
It then sets another variable (sensor_4) to hold the same thing
If the // were removed it would then print the value on the serial port
It requests something from a slave on the I2C bus
(what exactly is anyones guess as it will be dependent on the device attached)
The value returned is stored in sensor_1;

It then does the same for yet another mysterious device
and stores the result in sensor_2

Same for sensor_3

If any of the results returned by these sensors are higher than their respective "seuil" value
Then the CurrentSensor will be set to hold the address of the relevant sensor.

The code is incomplete (seuil_1 - seuil_4 are not defined anywhere)

Apart from reading there are absolutely NO ouput commands. So it seems to be an exercise in futility.

Thank you fr yr replies,

I don't undertand those lines:

if (x > y && x > v && x > z){
w=x;
}
if (y > x && y > v && y > z){
w=y;
}
if (v > y && v > x && v > z){
w=v;
}
if (z > y && z > v && z > x){
w=z;
}

thanks in advance

the whole code is:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <util.h>
#include <Wire.h>

byte sensorByte[2];
byte buffer[2];
int byteReceived = 0;

int sensor_1 = 0;
int sensor_2 = 0;
int sensor_3 = 0;
int sensor_4 = 0;

int x;
int y;
int v;
int z;
int w;

byte mac = {0x90, 0xA2, 0xDA, 0xFF, 0x2B, 0x8E};
IPAddress ip(192, 168, 32, 120);

unsigned int localPort = 8888; // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

EthernetUDP Udp;

void setup()
{
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Wire.begin(1); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
Serial.begin(9600);
}

void loop()
{
x = analogRead(0);
y = analogRead(1);
v = analogRead(2);
z = analogRead(3);

if (x > y && x > v && x > z){
w=x;
}
if (y > x && y > v && y > z){
w=y;
}
if (v > y && v > x && v > z){
w=v;
}
if (z > y && z > v && z > x){
w=z;
}
sensor_1 = w;
//Serial.println(sensor_1);

int packetSize = Udp.parsePacket();
if(packetSize)
{
IPAddress remote = Udp.remoteIP();

String str1 = String(sensor_1);
String str2 = String(sensor_2);
String str3 = String(sensor_3);
String str4 = String(sensor_4);

String message = String("C1 : " + str1 + " C2: " + str2 + " C3: " + str3 + " C4 : " + str4);
char tmp[50];
message.toCharArray(tmp, 50);

// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);

// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(tmp);
Udp.endPacket();
}

Wire.requestFrom(2, 2); // request 6 bytes from slave device #2
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_2 = word(buffer[1],buffer[0]);
}

}

Wire.requestFrom(3, 2); // request 6 bytes from slave device #3
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_3 = word(buffer[1],buffer[0]);
}
}
Wire.requestFrom(4, 2); // request 6 bytes from slave device #4
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_4 = word(buffer[1],buffer[0]);
}
}
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
sensorByte[0] = highByte(sensor_1);
sensorByte[1] = lowByte(sensor_1);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

the code master
for code slave:

#include <Wire.h>

byte sensorByte[2];

int sensor = 0;

int x;
int y;
int v;
int z;
int w;

void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
//Serial.begin(9600);
}

void loop()
{
x = analogRead(0);
y = analogRead(1);
v = analogRead(2);
z = analogRead(3);

if (x > y && x > v && x > z){
w=x;
}
if (y > x && y > v && y > z){
w=y;
}
if (v > y && v > x && v > z){
w=v;
}
if (z > y && z > v && z > x){
w=z;
}
sensor = w;
//Serial.println(sensor);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
sensorByte[0] = highByte(sensor);
sensorByte[1] = lowByte(sensor);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

I don’t understantd why usinf the seuil of sensor,
arduino uses is ethernet,I can’t receive the valuer of the sensor, !

I want start with short code just to read the values of my sensor

Plz any idea?
Thanks in advance

if (x > y && x > v && x > z){
  w=x;
}

That one says, if the value of a variable called x is greater than that of the value of a variable called y, and at the same time x is greater than v, and at the same time x is greater than z, take the value of x and put it in the variable w.

The following code is a VERY inefficient way of saying,
put the Maximum of (x,y, z) into the variable w

 if (x > y && x > v && x > z){
   w=x;
 }
 if (y > x && y > v && y > z){
   w=y;
 }
 if (v > y && v > x && v > z){
   w=v;
 }
 if (z > y && z > v && z > x){
   w=z;
 }

Thanks you jumboZa,

plz you understand these lines for code,

Wire.requestFrom(2, 2); // request 6 bytes from slave device #2
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_2 = word(buffer[1],buffer[0]);
}

}

Wire.requestFrom(3, 2); // request 6 bytes from slave device #3
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_3 = word(buffer[1],buffer[0]);
}
}
Wire.requestFrom(4, 2); // request 6 bytes from slave device #4
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_4 = word(buffer[1],buffer[0]);
}
}
}

any information would be greatly appreciated,I need some direction ,I'im biginner on arduino,

Thanks in advance.
Regards

I' have this code ,
is wasn't me who do it,
have y any idea to make it more effecient?
Thanks in advance.

I d'ont understand these lines,
Can someone explan me pzl:

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_2 = word(buffer[1],buffer[0]);
}

void requestEvent()
{
sensorByte[0] = highByte(sensor);
sensorByte[1] = lowByte(sensor);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

Thanks in advance
Regards

you are reason

the variable seuil is declared in this code ,

#include <SPI.h>
#include <OSCMessage.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <avr/wdt.h>

int sensor_1 = 0;
int sensor_2 = 0;
int sensor_3 = 0;
int sensor_4 = 0;

byte sensorByte[2];

int sensor_1_tmp = 0;
int sensor_2_tmp = 0;
int sensor_3_tmp = 0;
int sensor_4_tmp = 0;

int CurrentSensor = 0;

int byteReceived = 0;
byte buffer[2];

int seuil_1 = 150;
int seuil_2 = 250;
int seuil_3 = 300;
int seuil_4 = 250;

byte mac = {0x90, 0xA2, 0xDA, 0xFF, 0x2B, 0x8E};

IPAddress ip(192,168,32,100);
IPAddress ipOut(192,168,32,150);

unsigned int outPort = 13000;

EthernetUDP Udp;

boolean WaitFalse = false;

void setup()
{
Ethernet.begin(mac,ip);
Udp.begin(outPort);
Wire.begin(4); // join i2c bus (address optional for master)
Wire.onRequest(requestEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
sensor_4 = analogRead(0);

Wire.requestFrom(1, 2); // request 6 bytes from slave device #2
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_1 = word(buffer[1],buffer[0]);
}

}
Wire.requestFrom(2, 2);
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_2 = word(buffer[1],buffer[0]);
}
}

Wire.requestFrom(3, 2);
byteReceived = 0;

while(Wire.available()>0) // slave may send less than requested
{
byte receivedByte = Wire.read(); // receive a byte as character
byteReceived ++;

if (byteReceived == 1){
buffer[1] = receivedByte;
}

if (byteReceived == 2){
buffer[0] = receivedByte;
sensor_3 = word(buffer[1],buffer[0]);
}
}

if (sensor_1 > seuil_1 && CurrentSensor == 0){
CurrentSensor = 1;
}
if (sensor_2 > seuil_2 && CurrentSensor == 0){
CurrentSensor = 2;
}
if (sensor_3 > seuil_3 && CurrentSensor == 0){
CurrentSensor = 3;
}
if (sensor_4 > seuil_4 && CurrentSensor == 0){
CurrentSensor = 4;
}
if (CurrentSensor == 1) {
if(!WaitFalse){
OSCMessage msg("/sensor1");
msg.add(true); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = true;
Serial.println(“true”);
}
if(WaitFalse && sensor_1 < seuil_1 -100){
OSCMessage msg("/sensor1");
msg.add(false); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = false;
CurrentSensor = 0;
Serial.println(“false”);
}
}
if (CurrentSensor == 2){
if(!WaitFalse){
OSCMessage msg("/sensor2");
msg.add(true); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = true;
Serial.println(“true”);
}

if(WaitFalse && sensor_2 < seuil_1 -100){
OSCMessage msg("/sensor2");
msg.add(false); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = false;
CurrentSensor = 0;
Serial.println(“false”);
}
}
if (CurrentSensor == 3){
if(!WaitFalse){
OSCMessage msg("/sensor3");
msg.add(true); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = true;
Serial.println(“true”);
}

if(WaitFalse && sensor_3 < seuil_1-100){
OSCMessage msg("/sensor3");
msg.add(false); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = false;
CurrentSensor = 0;
Serial.println(“false”);
}
}
if (CurrentSensor == 4){
if(!WaitFalse){
OSCMessage msg("/sensor4");
msg.add(true); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = true;
Serial.println(“true”);
}

if(WaitFalse && sensor_4 < seuil_1-100){
OSCMessage msg("/sensor4");
msg.add(false); // signal top

Udp.beginPacket(ipOut, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
WaitFalse = false;
CurrentSensor = 0;
Serial.println(“false”);
}
}
}
void requestEvent()
{
sensorByte[0] = highByte(sensor_4);
sensorByte[1] = lowByte(sensor_4);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

this is the same code

Wire.requestFrom(2, 2);    // request 6 bytes from slave device #2

Really? Which part of that statement requests 6 bytes?

  1. Use code tags!!

  2. This code does not work

 while(Wire.available()>0)    // slave may send less than requested
 { 
   byte receivedByte = Wire.read(); // receive a byte as character
   byteReceived ++;
   
    if (byteReceived == 1){
     buffer[1] = receivedByte;
   }

It never will read the whole response from the slave device. >0 does not mean that the whole message is available nor can you assome that it will become available in the while loop]

  1. Help us to help you - lay out you code before posting and always use code tags!

Mark

Hi all,
what is meanning of this part of code

void requestEvent()
{
sensorByte[0] = highByte(sensor_4);
sensorByte[1] = lowByte(sensor_4);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

why we use the highByte and lowByte?
Thanks in advance.
Regards

why we use the highByte and lowByte

What does the documentation for those two say?

I don't know,but not me who made the program,
plz can you help me,I need some directions
Thanks in advance

Imanesirin:
Hi all,
what is meanning of this part of code

void requestEvent()
{
sensorByte[0] = highByte(sensor_4);
sensorByte[1] = lowByte(sensor_4);
Wire.write(sensorByte,2); // respond with message of 2 bytes
}

why we use the highByte and lowByte?
Thanks in advance.
Regards

I am thinking you have jumped into a project that exceeds your abilities, start small, learn and grow :slight_smile: , but in answer to your question........ if the sensor uses 16 bit numbers, but the arduino is using 8 bit numbers, it needs breaking into 2 pieces of 8 bits........ 8bits = 1 byte. The low byte is bits 0-7, the high byte is bits 8-15.

So for example Lowbyte = 0x14; Highbyte=0x54; The full number is actually 0x5414.

Not trying to confuse you, but you should be aware of Big Endian and Little Endian. Try Google.

The example number could be stored in memory as 0x14 0x54 or 0x54 0x14, and mean the same thing, you need to know which system you are working with.

So in your sample code, sensorByte[0] = 8 bits, sensorByte[1] = 8 bits...... but sensorByte = 16 bits.

You should read more about bits, bytes and words.

Regards,

Graham

Hi, Imanesirin, is there any reason you need this code?
Or are you trying to learn C++ and how to program Arduino?

Tom.... :slight_smile:

TomGeorge:
Everything runs on smoke, let the smoke out, it stops running......

Like it :wink: :grinning:

Graham