Binary to Digital

Hey guys

I have typed in binary codes in my sketch and want to use them for turning on and off an LED.
Can anyone help me with the code. I want a delay of 125ms b/w each binary digit.

For Eg: A-01000001
I want the LED like OFF-ON-OFF-OFF-OFF-OFF-OFF-ON

SagarDev:
Hey guys

I have typed in binary codes in my sketch and want to use them for turning on and off an LED.
Can anyone help me with the code. I want a delay of 125ms b/w each binary digit.

For Eg: A-01000001
I want the LED like OFF-ON-OFF-OFF-OFF-OFF-OFF-ON

You will be more likely to get help if you post the code you have so far.

I think this might do it for you.

for (byte mask=1; mask=0; mask<<1){ // 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0!
digitalWrite (pinLED & mask);
delay (125); // dumb delay, can work out a blink-without-delay method later, otherwise stuck in this loop for 2 seconds
}
digitalWrite (pinLED, 0);
for (byte mask=1; mask=0; mask<<1){

That won’t turn the first time. I think you meant:

for (byte mask=1; mask>0; mask<<=1){

Why would not the loop continue as long as the condition is not met?
Perhaps mask == 0 instead?
I could see mask >0 working as well.

The middle condition has to be true for the loop to do an iteration. If mask is equal to 1, then it isn't equal to 0, so the condition is false and therefor the loop doesn't run. The middle condition isn't the stop condition, it's a keep going condition.

Actually, since that is a single equal sign it would set mask equal to zero, which returns 0 and is therefor false making the loop not run. But had it been == it still wouldn't run.

void setup(){
  Serial.begin(115200);
  delay(250);
  
  Serial.println("Starting");
  
  for(byte mask=1; mask=0; mask<<=1){
    Serial.println("For loop executed");
  }
  
  Serial.println("Ending");
}

void loop(){}
Starting
Ending

Same output with mask==0

With mask>0

Starting
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
Ending

Also note in the code you posted you had as the third part mask<<1 where you need mask<<=1 as mask<<1 doesn’t really do anything useful. With for(byte mask=1; mask>0; mask<<1) you get an infinite loop of “For loop executed” since mask never changes.

Well, I was close. I think not too bad without actually trying it.

Thanks for your reply guys

But what should I do If I want to loop it?

Yet another option:

void setup(){
  Serial.begin(9600);
  delay(250);
 
  Serial.println("Starting");
 
  for(byte mask=1;  mask;  mask<<=1){
    Serial.println("For loop executed");
  }
 
  Serial.println("Ending");
}

void loop(){}

Output:

Starting
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
For loop executed
Ending

SagarDev:
Thanks for your reply guys

But what should I do If I want to loop it?

Loop what? the solutions you've been shown so far all involve for loops.

So Guys,

My Mega ADK mentions 16 MHz. So can I send 16 million 1's and 0's every second from my Arduino???

[Note:The ADK also takes input from a keyboard and processes it into Binary.]

SagarDev:
So Guys,

My Mega ADK mentions 16 MHz. So can I send 16 million 1's and 0's every second from my Arduino???

[Note:The ADK also takes input from a keyboard and processes it into Binary.]

I think the upper useful limit is about 115,200 ones and zeros a second (with lots of caveats).

Pretty much every digital device processes input into binary. Binary is the way computers and microcontrollers communicate and store value.

Normal serial communication is binary.

So does it give an output equal to the maximum Baud Rate i.e. 115200 ??

SagarDev:
So does it give an output equal to the maximum Baud Rate i.e. 115200 ??

No. Turning a LED on and off has nothing whatsoever to do with serial baud rates.

Okay I need a code with only Serial INPUT but output must be digital i/o 255 or 0.

Please Guys…

SagarDev:
Okay I need a code with only Serial INPUT but output must be digital i/o 255 or 0.

Please Guys...

Oh? So, you have 8 outputs?

No. One output where 0 is LOW and 1 is HIGH. A letter or number or symbol is typed which is converted to ASCII [8-bits] and is used to flash an LED.

The best you can do is 8 MHz with SPI and the data coming from the MOSI pin:

Serial.begin(115200); // higher speeds available, maybe not with IDE's Serial Monitor.
#include <SPI.h>
void setup(){
pinMode (10, OUTPUT); // needed for SPI master - SPI library may take of this already
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2); // check syntax of that
}
void loop(){
  if (Serial.available()>0){
  SPI.transfer(Serial.read() ); // read a byte, make MOSI send the data out
  }
}

SagarDev:
No. One output where 0 is LOW and 1 is HIGH. A letter or number or symbol is typed which is converted to ASCII [8-bits] and is used to flash an LED.

What you are describing is normal asynchronous communication. Asynchronous communication just adds a start bit and a stop bit to make it easier to keep the individual bytes (characters) separate.