Char to binary then to LEDs

Hello everyone,

Only just started playing with Arduino, but so far been having lots of fun.

I've become slightly stuck on how to convert char into binary, lets say I have some text 'hello world' etc.. & wanted to convert it to binary that would then output to 8 LEDs?

I've got the LED's all setup with 330's & working with a binary counter that I followed off a tutorial online. & just been adapting that so far.

If anyone could give me any pointers as where to look I would much appreciate it.

My plan after that is to get a feed coming from my twitter that would then change the LED's but I will look at that once I get this working.

Cheers all!

``````int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int pin12 = 12;

int bin = 0;
int led = 0;
int r = 1;

void setup()
{
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pin9, OUTPUT);
pinMode(pin10, OUTPUT);
pinMode(pin11, OUTPUT);
pinMode(pin12, OUTPUT);
}

void loop()
{
if(bin == 255)
{
r = 0;
}
if(bin == 0)
{
r = 1;
}

if( r == 1 )
{
bin += 1;
}
else
{
bin -= 1;
}

toLeds(bin);

delay(80);
}

void toLeds(int code)
{
digitalWrite(pin2, code & 1);
code = code >> 1;
digitalWrite(pin3, code & 1);
code = code >> 1;
digitalWrite(pin4, code & 1);
code = code >> 1;
digitalWrite(pin8, code & 1);
code = code >> 1;
digitalWrite(pin9, code & 1);
code = code >> 1;
digitalWrite(pin10, code & 1);
code = code >> 1;
digitalWrite(pin11, code & 1);
code = code >> 1;
digitalWrite(pin12, code & 1);
}
``````

Well for a start you need to learn how to use loops to stop you writing such turgid code as you do in the toLeds function. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

It is not sure what you want however. Do you want to convert the ASCII code into the matrix pattern for an LED matrix? Again an array look up table is what you need.

Hi Mike, thanks for the prompt reply & the link to arrays, looking now.

I have 8 led's all to hit one column of binary with a delay after each one if that makes sense?

Like 01101000 01100101 01101100 01101100 01101111 00100000 each column represented by one LED

Hope this is clear!

Yes an array is what you need, that link showed you how do it for 7 segment displays but for a matrix the principle is just the same. See:- http://www.thebox.myzen.co.uk/Workshop/LED_Matrix.html

Great, thanks for the pointers, will let you know how I get on.

The groovy thing for you is that the chars are already binary. While they may be displayed for your enjoyment as any number of things from letters to numbers, the processor only sees binary. So there's no conversion needed, it is already binary.

Hi everyone,

Getting really stressed out and cannot work out what to do, or even start. My server outputting the @ tweet just by itself at ‘http://tidyworks.co.uk/arduino%20get%20tweet/tweets.php

On Arduino I have the code which works fine (I will attach at end) that pulls that tweet and you can see it on the serial.

So here is where I am stuck, how do I get that tweet to change my 8 LED’s which I currently have working in a binary counter?

*I couldn’t get the LED matrix to work on my setup. =/

Quite stressed out and feel like giving up.

Cheers for any help

``````#include <SPI.h>
#include <Ethernet.h>

// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:

// Set the static IP address to use if the DHCP fails to assign

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /arduino%20get%20tweet/tweets.php");
client.println("Host: tidyworks.co.uk");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
while(true);
}
}
``````

So here is where I am stuck, how do I get that tweet to change my 8 LED's which I currently have working in a binary counter?

Change them to what? HOW should the tweet affect the LEDs?

TidyWorks: So here is where I am stuck, how do I get that tweet to change my 8 LED's which I currently have working in a binary counter?

*I couldn't get the LED matrix to work on my setup. =/

As far as I can see, what you're doing currently is reading each character of the HTTP response and simply echoing it to the serial port. You don't seem to have any code relating to LEDs. So you need to solve two problems:

You need to buffer the HTTP response, parse it to extract the data which you're going to use to control the LEDs and then process that data to derive the desired LED states. To design the code to do that you need to know what HTTP response you're getting (you can see that, but we can't) and understand how the LED states are derived from it.

You need to design and assemble the LED circuit and power supply, and write code which lets you turn the LEDs on and off. I suggest you develop this using a completely separate sketch that writes hard-coded values to the LEDs and only try to incorporate that into your project once you have got the LED control working.

Thanks for the responses.

The code at the start of this thread is the code to control the LED’s, although I have changed it to loops.

That tweets.php extracts just the code I want, which is the ‘@baquty does this work?’

I want to convert that into binary then have each bit switch on one led if its 1 & 0 for off.

How do I get read char from the tweets.php and change it to binary? someone said it already is binary, how do I set it to change each led? I haven’t found any tutorials online that link to what I’m trying to do.

Hopefully this is clear, & sorry for the dumb questions I’ve only just started learning C & finding it quite difficult.

Thanks.

If anyone’s interested, this is the code for the tweets.php

``````<?php

function getConnectionWithAccessToken(\$oauth_token, \$oauth_token_secret) {
\$connection = new TwitterOAuth("", "", \$oauth_token, \$oauth_token_secret);
return \$connection;
}

\$connection = getConnectionWithAccessToken("", "");

\$content = \$connection->get("statuses/mentions");

\$single = \$content[0]->text;

print_r(\$single);

?>
``````

If you want to control leds with binary strands, you will need to look into bitRead(). Im not going to do it for you, but there are others who wanted to do both of the things your talking about. Its actually quite common, they want get data from web and control something with the Arduino. Do a search in the forum and if you word it correctly, you will get many possible topics.

That tweets.php extracts just the code I want, which is the '@baquty does this work?'

I want to convert that into binary then have each bit switch on one led if its 1 & 0 for off.

How many LEDs do you have? The string has 23 characters. To use that string to control LEDs, you'd need 184 of them.

If you want each character to control 8 LEDs at a time, you need to define how long each character is to be displayed.

The data is stored internally in binary. There is no need to convert a character to binary. Converting a string to binary isn't needed, either, since a string is a NULL terminated array of chars, which are already stored in binary.

TidyWorks: That tweets.php extracts just the code I want, which is the '@baquty does this work?'

I want to convert that into binary then have each bit switch on one led if its 1 & 0 for off.

How do I get read char from the tweets.php and change it to binary?

The PHP code doesn't run on the Arduino - you need to know what HTTP response the Arduino receives, and which part of that response contains the data you want to use to control the LEDs.

I guess the response is textual and will contain the required LED states encoded in a text string in some way. If so, you will need to parse the response to convert it from a textual format into a HIGH/LOW state for each LED. For example you could decide to encode the LED states as a sequence of '1' and '0' ascii characters. In that case once you have located these characters within the response you can just compare each one to '1' and '0' and set the LED output state accordingly. However, there's a lot of speculation here because we have no idea what your HTTP response will contain or how the LED states are represented within it.