I have project for the university and I want to transmit data between tow around and , when I set the code I marge it to be transmit and receive, for the first time it work but when I turn off everything and try to do it again never work can any one help me please.
The code:
#define SOLARPIN A0
#define THRESHOLD 210
int ambientReading = 0;
void setup() {
pinMode (LASERPIN, OUTPUT);
Serial.begin(9600);
pinMode(SOLARPIN, INPUT);
Serial.begin(4800);
}
void loop() {
if (Serial.available() > 0) {
String myText = Serial.readStringUntil('\n'); // read the incoming string until newline character is detected
int length = myText.length();
int ar[length];
int m;
int bits[8];
for (int i = 0; i < length; i++) {
ar[i] = int(myText.charAt(i)); // convert each character to its ASCII value and store it in an array
}
for (int n = 0; n < length; n++) {
m = ar[n];
int z;
int bin[7];
int newbin[7];
// convert decimal value to binary
for (z = 0; z < 8; z++) {
bin[z] = m % 2;
m = m / 2;
}
// reverse the order of binary digits
for (int j = 7; j >= 0; j--) {
newbin[7-j] = bin[j];
}
// set the first bit to HIGH to indicate the start of a new character
newbin[0] = HIGH;
// transmit each bit of the binary value using the laser
for (int i = 0; i < 8; i++) {
digitalWrite(LASERPIN, newbin[i]);
delay(10);
}
// turn off the laser after transmitting all bits of the character
digitalWrite(LASERPIN, LOW);
// add a delay between characters
delay(100);
}
}
int reading = analogRead(SOLARPIN);
int bits[8];
//Listening for the start bit
if (reading > THRESHOLD) {
for (int i =0; i<8 ; i++) {
if (analogRead(SOLARPIN) > THRESHOLD) {
bits[i] = 1 ;
}
else {
bits[i] = 0;
}
delay(10);
}
int m = 0;
for (int j =1; j <8; j++) {
if (bits[j] ==1) {
m = m + (1<<(7-j));
}
}
char n=m;
Serial.print(n);
}
}