Hello ![]()
I have a project with two arduino cards witch need to communicate a int (0 to 128) between them.
I tried different ways to make this working and it steel not working
(433mhz, 2.4ghz) and now I am trying to transfer an int by optical fiber. But, I would like to send and receive the data by digital (0,1) and not by analog.
So : - The arduino receives an int by serial port, then it converts this int to a binary array and send this binary array to the led -> 1st arduino
- The arduino receives the the light by a photoresistor (connected to analog port of the arduino) and when the value is over "x" > the bit is 1 else the bit is 0
I searched on the internet and I found this :
const byte numPins = 7;
byte pins[] = {13, 14, 15, 16, 17, 18, 19};
void setup() {
 Serial.begin(115200);
}
void loop() {
 while(!Serial.available()); // Do nothing until serial input is received
 byte num = Serial.read(); // Get num from somewhere
 for (byte i=0; i<numPins; i++) {
  byte state = bitRead(num, i);
  digitalWrite(pins[i], state);
  Serial.print(state);
 }
 Serial.println();
}
Original post here : Convert int to binary Array - Programming Questions - Arduino Forum
I think it will work for me, I edited this a bit :
const byte numPins = 7;
const int ledPin = 6;
void setup()
{
 // put your setup code here, to run once:
Serial.begin(9600);
}
void loop()
{
 // put your main code here, to run repeatedly:
while(!Serial.available()); // Do nothing until serial input is received
byte num = Serial.read(); // Get num from somewhere
for (byte i=0; i<numPins; i++)
{
  byte state = bitRead(num, i);
  //digitalWrite(pins[i], state);
  delay(10);
  if(state == 1)
  {
   digitalWrite(ledPin,HIGH);
  }
  else
  {
   digitalWrite(ledPin,LOW);
  }
  //Serial.print(state);
 }
}
And I have a problem with the receiver, because I dont know how to convert the binarry array to a int.
Here is my RX code :
int sensorPin = 3;Â Â Â //Sensor witch will receive the data from the led
int limit = 1023/2;Â Â //Var witch will define when the signal is "1" or "0"
int received;Â Â Â Â Â //Var witch will stock the value from the analog pin
const byte numPins = 7; //How many bits ? (2^numPins)
int bits[7];
void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
 // put your main code here, to run repeatedly:
while(analogRead(sensorPin) < limit){} //while sensor pin < limit, do nothing
received = analogRead(sensorPin);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â //Those whiles sync the data receiving
while(analogRead(sensorPin) > limit){} //while sensor pin < limit, do nothing
for(byte i=0; i<numPins; i++)
{
 received = analogRead(sensorPin);
 if(received < limit)
 {
  bits[i] = 0;
 }
 else
 {
  bits[i] = 1;
 }
}
}
Thanks for your help ![]()