Hi everyone,
I have a question about binary coding with the Arduino. Basically, I want to be able to have an LED blink out the binary value of numbers or short phrases. For instance, if I wanted to show a 9, I would have the LED blink out high-low-low-high. That would be simple enough to write in manually, but if I wanted to use bigger numbers, such as 99 (1100011) or short phrases like 'Hello World' (01001000 01100101 0110....), it would obviously be very difficult to manually type in all the numbers. My goal in the end is to blink out the binary values for a few words and numbers on an LED at a rate of 2 bits per second. Does anyone have any example codes or know of any tutorials for such a thing? Really any help would be awesome.
Thanks
~Josh
How would you blink out 000000000? Does the LED ever turn on?
How would you know when you blinked a zero?
aarg,
That's why I have a set rate, so to blink out 0000000000 I would just leave the LED off for 5 seconds, since there are 10 bits and the rate it blinks at is 2 bits per second.
outsider,
I hadn't thought of that, but that's a really good point! Perhaps I could have it send out a few rapid flashes, followed by the binary information, followed by another few rapid flashes. That way I could tell when the message began and ended. Thanks for pointing that out!
Hello,
What about using Morse code?
guix,
I had considered Morse code, but I would really prefer binary, despite it being much more difficult. However, should binary prove too difficult, I may just end up using Morse code.
How about 2 LEDs, yellow for 0, red for 1?
That is an option, but that doesn't help me with my original problem, how to break things into their binary values in the first place.
Josh_Blackburn:
it would obviously be very difficult to manually type in all the numbers.
But you don't have to type it out in binary. You have to realize that it doesn't matter how you type it out in your program, it will be stored in binary in memory. So you can do things like this:
byte b = 9;
for(byte i = 0; i<8; i++){
Serial.print("the ");
Serial.print(i);
Serial.print("th bit of b is a : ");
// if the i'th bit is a 1
if(b & (1 << i)){
Serial.println("1");
}
else {
Serial.println("0");
}
}
And it will run through the bits of the number 9 in binary.
Josh_Blackburn:
Hi everyone,
I have a question about binary coding with the Arduino. Basically, I want to be able to have an LED blink out the binary value of numbers or short phrases. For instance, if I wanted to show a 9, I would have the LED blink out high-low-low-high. That would be simple enough to write in manually, but if I wanted to use bigger numbers, such as 99 (1100011) or short phrases like 'Hello World' (01001000 01100101 0110....), it would obviously be very difficult to manually type in all the numbers. My goal in the end is to blink out the binary values for a few words and numbers on an LED at a rate of 2 bits per second. Does anyone have any example codes or know of any tutorials for such a thing? Really any help would be awesome.
Thanks
~Josh
This will do what you want (I think):
#define LED 13
void send_binary (char *data)
{
uint8_t x;
uint8_t bits;
for (x = 0; x < strlen (data); x++) {
bits = 8;
while (bits--) {
(data[x] & _BV(bits)) ? digitalWrite (LED, HIGH) : digitalWrite (LED, LOW);
delay (250);
digitalWrite (LED, LOW);
delay (250);
}
delay (1500); // inter-char delay
}
}
void setup (void)
{
char *str = "Now Is The Time For All Good Men To Come To The Aid Of The Party";
pinMode (LED, OUTPUT); // using built-in LED
send_binary (str);
}
void loop (void)
{
// nothing
}
Although it's VERY difficult to see / figure out what's being sent. Have you thought of using Morse code? It's much easier to distinguish "long-short-long-short" than it is to distinguish "blink / no blink".
Hope this helps.
Yes, that's exactly what I wanted to do, Thank you very much!
Josh_Blackburn:
Yes, that's exactly what I wanted to do, Thank you very much!
You may also wish to change "bits" from 8 to 7 because ASCII characters are always 0x7F or below (no sense in always sending an "LED OFF" for the 8th. bit that will always be low).
And here's an old sketch called "poor man's GUI" to play with, it's useful for debugging without Serial.print.
// Poor man's GUI
byte led = 13;
void pmGUI(byte led, int num)
{
int interval = 350, i;
if(num == 0){
num = 4;
interval = 50;
}
for(i = 0;i < num;i++){
digitalWrite(led,HIGH);
delay(50);
digitalWrite(led,LOW);
delay(interval);
}
delay(600);
}
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
Serial.println(" Type a number from 0 to 999, then <ENTER> ");
Serial.println(" A \"stutter\" = zero \n");
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int num = Serial.parseInt();
// look for the newline. That's the end of your
// sentence:
if (Serial.read() == '\n') {
}
if(num > 99) pmGUI(led,num / 100);
if(num > 9) pmGUI(led,num % 100 / 10);
pmGUI(led,num % 10);
}
}
I thought he wanted to blink arbitrary 8-bit binary values, not just zero-terminated ASCII character arrays.
EDIT: Added code to blink a memory block at a specified address and length.
#include <limits.h>
#define NUM_ENTRIES(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
const uint8_t pinLED = 13;
const uint8_t LED_OFF = LOW;
const uint8_t LED_ON = HIGH;
void blink_it(uint8_t const value)
{
// ... the question still remains, how to indicate 0 ...
const unsigned long tmsDELAY = 250UL;
const unsigned long tmsINTERVAL = 1500UL;
const size_t MASK = (1 << ((sizeof(value) * CHAR_BIT) - 1));
for ( size_t mask = MASK; mask; mask >>= 1 )
{
digitalWrite(pinLED, ((value & mask) ? LED_ON : LED_OFF));
delay(tmsDELAY);
digitalWrite(pinLED, LED_OFF);
delay(tmsDELAY);
}
delay(tmsINTERVAL);
}
void blink_it(const uint8_t* p_src, size_t length)
{
for ( size_t n = length; n--; p_src++ )
{
blink_it(*p_src);
}
}
void loop()
{ }
void setup()
{
pinMode(pinLED, OUTPUT);
const uint8_t block[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
blink_it(block, NUM_ENTRIES(block));
}