Just started programming on Arduino the other day, and can't quite figure out a few things.
So here's the flow of what I'm trying to accomplish.
-Prompt user for message to transmit (Done)
-Store input as String (Done)
-Convert String to binary (Not done)
-Transmit binary with LED (Done)
-Receive binary with Photoresistor (Done)
-Convert binary to String (Not done)
-Print String to LCD (Done)
I can create a generic "int myMsg={HIGH, LOW, HIGH, LOW}," send that to the LED, receive it on the other side, and print it to the Serial Monitor as 1010.
I think I might be going about this in the wrong way.
I used a bitRead loop to convert a String to binary and print that to the monitor one digit at a time, but I haven't been able to store the entire String to an array of binary octets. I'm pretty new to this language.
From what I've gathered, converting a character to ascii, and ascii to binary is built in, so I shouldn't need to include a library for that conversion.
Using bitRead on the letter 'a' returns its ascii value, 97, in binary, 01100001.
I don't how to create an array of octets, or if that's even the direction I should be going.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
String message = "abc";
int bytes[24];
for(int i=0;i<message.length(); i++){
char character = message.charAt(i);
for(int i=7; i>=0; i--){
bytes[i] = bitRead(character,i);
Serial.print(bytes[i]);
delay(100);
}
}
delay(5000);
Serial.println("");
}
Each character IS an octet (same thing as a byte). So you are already doing a conversion to highs and lows, as you put it.
But, some things to note - for every character you read from serial, you're writing to the first 8 bytes of the "bytes" array. As it is written now, you are only writing to those first 8 bytes. You could change this from
for(int j=7; j>=0; j--){
bytes[j + i * 8] = bitRead(character,j);
Serial.print(bytes[j + i * 8]);
}
This way, you will actually write past the first 8 elements of bytes, so you can actually encode multiple characters.
Doing that, you'll still run into a problem with the second loop, which is that you are emitting all 256 "highs" and "lows", but you're not necessarily writing all 256 highs and lows.
All this to ask, why are you writing to an intermediate array (bytes) and not just emitting immediately? Do you plan on doing something more with that data?
binaryfissiongames:
All this to ask, why are you writing to an intermediate array (bytes) and not just emitting immediately? Do you plan on doing something more with that data?
Because I'm used to MATLAB, and I'm spoiled. I like being able to check an array after its loops run to make sure I did it right. I can see that's not a common thing on this side of the house.
I'll play with the stuff others suggested and see if it comes together.
//Binary Transmitter
#define laser 12
String message; //initiate variable
String prompt = "What is your message?"; //initiate prompt
String confirm = "Your message is "; //repeats message
int bytes[256];
int data[256];
void setup() {
Serial.begin(9600); //initiate serial monitor
pinMode(laser, OUTPUT);
}
void loop() {
Serial.println(prompt); //ask for input
while (Serial.available() == 0); { //wait for input
}
message = Serial.readString(); //write input to variable
Serial.println(confirm); //repeat message
Serial.println(message); //
digitalWrite(laser,HIGH);
delay(10);
digitalWrite(laser,LOW);
for(int i=0;i<message.length(); i++){
char character = message.charAt(i);
for(int i=7; i>=0; i--){
bytes[i] = bitRead(character,i);
Serial.print(bytes[i]);
if(bytes[i]==1){
digitalWrite(laser,HIGH);
}
else{
digitalWrite(laser,LOW);
}
delay(50);//same delay per bit as transmitter (10ms is too short for LED)
}
Serial.println("");
}
digitalWrite(laser,LOW);
Serial.println("");
}
//Binary Receiver
#define receiver A0
#define tolerance 30
int ambient = 0;
void setup() {
pinMode(receiver, INPUT);
Serial.begin(9600);
ambient = analogRead(receiver);
}
void loop() {
int reading = analogRead(receiver);
int bits[8];
// Listening for the start bit
if (reading > ambient + tolerance) {
delay(20);//longer delay than transmitter to stop the start bit from being mistaken for a data bit
for(int i=0;i<4;i++){ //maximum characters controled here
for (int i = 0; i < 8; i++) {//octet loop
if (analogRead(receiver) > ambient + tolerance) {
bits[i] = 1;
}
else {
bits[i] = 0;
}
Serial.print(bits[i]);
delay(50);//same delay per bit as transmitter (10ms is too short for LED)
}
Serial.println("");
}
Serial.println("");
}
}
binaryfissiongames:
All this to ask, why are you writing to an intermediate array (bytes) and not just emitting immediately? Do you plan on doing something more with that data?
zjcrawford:
Because I'm used to MATLAB, and I'm spoiled. I like being able to check an array after its loops run to make sure I did it right. I can see that's not a common thing on this side of the house.
Arduinos have (very) limited RAM. This is why, rather than writing your values to an array, it is best to just print them directly to the Serial monitor.
If I were to recite a list of numbers, dozens of numbers long, for you to write down, would you wait for me to finish reciting the entire list before you began to write? Of course not! You would write down each number as soon as you heard it. Why? Because otherwise your memory would be overwhelmed with all the numbers.
Same thing with an Arduino.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
//Binary Receiver
#define receiver A0
#define tolerance 30
int ambient = 0;
void setup() {
pinMode(receiver, INPUT);
Serial.begin(9600);
lcd.begin(16,2);
ambient = analogRead(receiver);
}
void loop() {
int reading = analogRead(receiver);
int msg[4];
int bits[8];
byte binary=0b00000000;
char character;
// Listening for the start bit
if (reading > ambient + tolerance) {
lcd.clear();
delay(20);//longer delay than transmitter to prevent the start bit from being mistaken for a data bit
for(int i=0;i<64;i++){ //maximum characters controled here
for (int i=7;i>=0;i--) {//octet loop
if (analogRead(receiver) > ambient + tolerance) {
bitWrite(binary,i,1);
}
else {
bitWrite(binary,i,0);
}
delay(40);//same delay per bit as transmitter (10ms is too short for LED)
}
char character=binary;
Serial.print(character);
lcd.print(character);
}
Serial.println("");
}
}
odometer:
Arduinos have (very) limited RAM. This is why, rather than writing your values to an array, it is best to just print them directly to the Serial monitor.
If I were to recite a list of numbers, dozens of numbers long, for you to write down, would you wait for me to finish reciting the entire list before you began to write? Of course not! You would write down each number as soon as you heard it. Why? Because otherwise your memory would be overwhelmed with all the numbers.
Same thing with an Arduino.
I understand that, and I did take it that way. The process breaks down on long messages because the sender and receiver are falling out of sync. I used to work in communications, so I'm familiar with the need for unified timing to keep everything on the same beat, but I haven't studied actually programming it yet.