Sending incoming data as Binary to led or light.

Hi,

I have a problem.

I am working on Project and the project is transmitting data through led or light.

I don’t know how to insert incoming data form Arduino console. and don’t know how to even create a transmitter and receiver.

I am a complete beginner at C++ (and Arduino) and have tried to write some code and this what I come up with. I tried to use a bit method where the transmitter data convert data (like a word or sentence) to bits and sends it out to the receiver and the receiver reassembles the incoming data to words.

#define Laser 13
void setup() {
  pinMode(Laser, OUTPUT);
  Serial.begin(9600);
}

void loop() {


  String incoming = "Hello World";
  //start bit
  digitalWrite(Laser, HIGH);
  delay(200);
  digitalWrite(Laser, LOW);
  for (int i = 0; i < incoming.length(); i++) {

    char ac = incoming.charAt(i);

    for (int i = 7; i >= 0; i--) {
      byte bytes = byte(ac, i);
      byte bytess = bytes, BIN;
      digitalWrite(Laser, bytess);
      delay(200);
    }

  digitalWrite(Laser, LOW);

  delay(1000);
  }
}

Thanks a lot.

byte bytes = byte(ac, i);
      byte bytess = bytes, BIN;

Try bitRead instead

I have already tried but it didn't work.

the laser has turned on once wich

  digitalWrite(LASERPIN, HIGH);
  delay(200);
  digitalWrite(LASERPIN, LOW);

caused it.

jwank:
I have already tried but it didn’t work.

Well, for most definitions of the word, the junk you posted won’t “work” either.

Give bitRead another try

well like I said I have no clue in c++. If it is possible can you write a “good” code down?
I would really I appreciate it if did.

You just need to isolate each bit in “ac” using bitRead repeatedly, and turn the LED on or off appropriately.

I don’t know what the good code would be, because I don’t know what your serial protocol is.

Wouldn't it be a lot simpler to send the data as a Serial transmission. For example use SoftwareSerial with its Tx on the pin connected to the laser and just do something like

mySerial.print("hello world");

With SoftwareSerial you have the option to invert the signal which would probably be a good idea so that the laser is OFF when there is nothing to send. With this option you would also need to use SoftwareSerial at the receiving end.

...R

I really want to do that but there is a tiny tiny problem which is I am new at programming and what I wrote was by searching in documents which i didn’t understand much of it.

there is a tiny tiny problem which is I am new at programming

If that’s the immediate problem, fix it first.

I am new at programming C/C++*

altho you didn’t help but thanks for your time :).
I have modified the code and it worked

int laser = 13;

void setup() {                
  pinMode(laser, OUTPUT);     
  Serial.begin(9600);
}

void loop() {
  
  byte input;
 
  while (Serial.available() > 0) {
    input = Serial.read();
    
    digitalWrite(laser, HIGH);  
    delay(10);
    digitalWrite(laser, LOW);


    for (int i = 0; i < 8; i++) {
      digitalWrite(laser, (input & (1 << i)) >> i);
      delay(100);
    }
    digitalWrite(laser, LOW);
    
    delay(10);
  } 
}

Congratulations - you reinvented bitRead.

thanks, and you don't have to be rude about it because I am a beginner if I was a pro then could have made fun of me. anyways thanks for your time.

Pace out.

If I send F (which will be transmitted as: 0100 0110 = ASCII Code of F) from the InputBox 0f Serial Monitor (Fig-1), what will be the ON/OFF conditions of L (built-in LED connected at DPin-13 of Arduino) in response to your sketch of Post#9?

Yeah, for future reference, never, ever write “it didn’t work” without additional detail.

It’s the proverbial red rag to the proverbial bull.