Showing posts with label led. Show all posts
Showing posts with label led. Show all posts

Tuesday, August 23, 2016

buttonFlash - game made with Raspberry Pi and Arduino communicating using NRF24L01 modules

IMPORTANT: The code in this blog post is the original code which does work but may not have all the enhancements. If you're building your own I recommend grabbing the code from GitHub.
GitHub link: https://github.com/winkleink/buttonFlash

Update: 19th September:
Took buttonFlash to the Cambridge Raspberry Jam on the 17th of September and it got a proper outing.




UPDATE: 25th August:
Made Raspberry Pi program look prettier
Added sound effects


A few years ago I thought it would be fun to come up with an outdoor game that would get people running around. The Whack-a-Mole/reflexes button game came to mind and this is the result. A game where you have to press buttons as quickly as possible.



Since then a great Raspberry Pi version called Whack-a-Pi was done by +ForToffee
Here are his build instructions and videos. https://fortoffee.org.uk/2016/03/whack-a-pi/

Whack-a-Pi was in constant play at the Raspberry Pi 4th Party.


The concept for buttonFlash is similar to Whack-a-Pi but with the ability for the buttons to be much further apart.  Goal is to get people running around.  With the buttons further apart there was no way I was going to be managing physical wires as it would be a trip hazard and I didn't want to have to manage that many wires.  So, wireless communication between the base and the buttons seemed the obvious choice.

Most fields don't have wifi.  I could bring along my trusty Vocore and create a local WiFi hot spot but again I want this to be simple to set up and use.

VoCore.  Basically guts of a Wifi router

So, I thought the NRF24L01 would do the trick.  They're a really cheap transceiver. They can transmit and receive signals making them ideal for short range two way communication.

NRF24L01

This current UK eBay listing is for five NRF24L01 for £5.99
Also, there are stable libraries for Arduino and Raspberry Pi available.

On the Arduino I used the RF24 library.  If you've the latest version of the Arduino IDE you can install this through the library manager or if you prefer you can go old school and download it from the GitHub repository at the link above. More details on using the NRF24 with Arduino

On the Raspberry Pi I used the following Library https://github.com/BLavery/lib_nrf24

The Arduino side was relatively straightforward to get working while the Raspberry Pi side had me stumped for a while.  Then earlier this year I had a breakthrough when I met +Elliot Pittam.  He had seen the first Wimbledon Raspberry Jam and was interested in coming along.  On email we discussed what we were working on and Elliot said he was using the NRF24L01 with the Pi and Arduino.  We then met at a HackWimbledon event where he showed me his set up and shared his working code. Elliot helped me sort my circuit and pointed me to 2 great videos on using the NRF24L01 with both Arduino and Raspberry Pi.


Tutorial 34 - Part 1

Tutorial 35 - Part 2

With Elliot's assistance I successfully had the Raspberry Pi communicating with the Arduino.


Many of the example I found did not clearly identify which pins on the Arduino or Raspberry Pi were connected to which pins on the NRF24L01. Therefore, below is the wiring I used for the Raspberry Pi and the Arduino with the code provided.  So, it all matches up nicely.

NRF     Pi GPIO  Pin
VCC     3.3V       1
GND     GND        6
CSN     GPIO8     24
CE      CPIO17    11
MOSI    GPIO10    19
MISO    GPIO9     21
SCK     GPIO11    23

NRF           Arduino
VCC     3.3V
GND     GND
CSN     Digital 10
CE      Digital 9
MOSI    Digital 11
MISO    Digital 12
SCK     Digital 13 

For the Arduino to keep it compact I went with Arduino Nano compatible board on a small 170 pin breadboard. Easy build and easy maintenance.

Soldering on the headers to the Arduino Nano
All 5 completed

The Arduino Nano Compatible with a suitable short USB cable are ~£4.50 from the UK but can be ~£2.50 if time isn't an issue and you're happy to order from China/Hong Kong.
It's important to know these compatibles use the CH340G serial chip which is different to the Arduino branded.  You will need additional drivers to get these working with some computers.

To make each of the buttons base I needed a suitable container to hold the button and the rest of the electronics and of course the button.  I'd seen these great 60mm buttons with LEDs built in so I order some.  Again from eBay UK these are about ~£7.00 for five.  

RED 60mm LED button

The ones I ordered are 12V rated which is the power for the LED.  Opening it up these things just have a standard LED and a current limited resistors. The resistor was a 460 Ohm for the 12 volts. Without knowing the specific LEDs and their voltage drop and maximum current I played it safe and replaced the 460 Ohm with 150 Ohm resistors.  The buttons did light very dimly with the 460s but were far brighter with the 150 Ohm.  Below is the button being taken apart.

button in place 

unscrew the button/LED assembly

pull out the LED (like Christmas lights)

remove LED with resistor on Anode (+ side, other goes to GND)

solder on replacement resistor and put it all back together

To test the LED at each stage I ran an Arduino with the blink sketched and wired the LED across pins GND and 13.  Once all reassemble with the wires attached again I tested to make sure I didn't mess things up in putting it back together.

Next, for each Arduino I wired the NRF24L01 as per above.  The LED was wired to Pin 2 and Button was wired to Pin 3. In code I activated the internal pull up resistor so the button is held high until you press it and then it goes low.


it all wired up.

As the Raspberry Pi needed to send a message to the Arduinos to let them know which one is to activate their button and wait for it to be pressed before replying I came up with a simple message that is extendable.  Basically, the number of the Arduino followed by 'P' Examples: 1P, 2P, 3P, 4P, 5P
This would be the message sent out.
All Arduino would receive it and then using a simple IF statement would decide if it means they are to be activated. If not go back to listening. If yes light the button and wait for it to be pressed. Once pressed reply to the Raspberry Pi that the button has been pressed. 
In this way the Arduino are dumb.  They only respond to a request for the button and reply it's been pressed. They don't care if they are the first, second, third,... button to be pressed in the game.  All that is managed at the Raspberry Pi.

To make it easier to setup and to test I included a test mode.
When you press 't' it sends the test message and all the Arduino flash their LEDs 5 time.  Then Arduino 1 (as there will  always be at least 2 Arduino) replies that it is completed.
The Raspberry Pi doesn't do anything with this message as the test is to see if it is sent and replied to which the users sees in the console.
This means Button/Arduino 1 has slightly different code to all the rest of the buttons.

Button/Arduino 1 code with the extra bit for Arduino 1 only highlighted in RED
For each Button/Arduino the text highlighted in BLUE needs to be changed to matched the relevant reference in the Raspberry Pi list. This text is the same in all 3 locations so you can do a Find/Replace to do it in one go.

#include<SPI.h>
#include<RF24.h>

//ce. csn pins
RF24 radio(9, 10);

int buttonPin = 3;
int ledPin = 2;
int x;

void setup(void) {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  while (!Serial);
  Serial.begin(9600);

  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setChannel(0x76);
  radio.openWritingPipe(0xF0F0F0F0E1LL);
  const uint64_t pipe = 0xE8E8F0F0E1LL;
  radio.openReadingPipe(1, pipe);

  radio.enableDynamicPayloads();
  radio.powerUp();

}

void loop() {
  radio.startListening();
  Serial.println("Starting Loop, Radio on 1P.");
  char receivedMessage[32] = {0};
  if (radio.available()) {
    radio.read(receivedMessage, sizeof(receivedMessage));
    Serial.print("Received Message: ");
    Serial.println(receivedMessage);
    Serial.println("Turning off the radio.");
    radio.stopListening();

    String stringMessage(receivedMessage);
// Actual action if asked for button press
    if (stringMessage == "1P") {
      digitalWrite(ledPin,HIGH);

      while (digitalRead(buttonPin) == HIGH){
            }
      digitalWrite(ledPin, LOW);      
      Serial.println("looks like they want a string");
      const char text[] = "Node 1P - Hello";
      radio.write(text, sizeof(text));
      Serial.println("We sent our message: " + String(text));
    }

// Test scenario    
    if (stringMessage == "TEST") {
      for(x=0; x < 5; x++){
      digitalWrite(ledPin,HIGH); 
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
      }
      Serial.println("Test completed");
      const char text[] = "Test done";
      radio.write(text, sizeof(text));
      Serial.println("We sent our message: " + String(text));
    
    }

  }

  delay(100);
}

On the Raspberry Pi side the code is a little more interesting as it sets up the NRF24L01 and then creates a graphical window using pygame and finally runs the game.  At this stage it displays Score and High Score.




The steps to get the Raspberry Pi ready are:
Wire up the NRF24L01 as per above
Use the Raspberry Po configuration program to enable SPI (you have to reboot after this)
Then run the following commands.


sudo apt-get update
sudo apt-get upgrade 
sudo apt-get install python-dev python3.dev -y

I'm assuming you're in hour home directory for the rest of this.

mkdir buttonFlash
git clone https://github.com/Gadgetoid/py-spidev
cd py-spidev
sudo python setup.py install
sudo python3 setup.py install

cd ..

git clone https://github.com/BLavery/lib_nrf24
cd lib_nrf24/

cp lib_nrf24.py ~/buttonFlash
cd ..

You should now have SPI working with the python SPI libraries installed for Python 2 and 3 as well as the required lib_nrf24 for working with the NRF24L01 from python.
Finally the library file is copied to the buttonFlash directory.

Copy the Raspberry Pi code into the buttonFlash directory and run it.

All the necessary files and code are available on GitHub https://github.com/winkleink/buttonFlash


sudo python3 buttonFlash.py3

Yeah. Now it all technically works
Here is a short example of it running after the initial build.



With the electronics sorted and the programs functioning the next requirement was a housing for the buttons.  I thought I found the perfect one when I got this spaghetti container from Home Bargains.   It all fit comfortable and the button was really secure.



Only problem was when I went back to get more they were out of stock with no idea when/if more would be coming in.  Then I saw the Pringles can and tried it out and it worked.  Pringles cans for the win.



Oh, and I had to eat another 4 cans of Pringles to get the 5 required.  



As this is designed to be played indoors or outdoors I needed to fina a way to make them stable when placed in a field.  I went super simple and bought 150mm bolts with nuts and passed them through the bottom of the Pringles cans.  You'll notice I numbered each can and there is also a colour listed.  I bought 5 different coloured breadboards so I could tell which Button/Arduino was in each can.  
You can see the bottoms of the tubes are deformed a bit.  Ends up Pringles cans aren't super strong.  Means I have to re-tighten the nuts as they work lose.  Wonder if an application from a glue gun will sort this out.


bottom of each Tube with bolt, number and colour 

all the bits outside the can.

If you watched the video at the top or the video at the bottom you might have noticed the Pringles cans have a bit of a base to keep them stable for indoor use.  In the rush to push the buttons the Pringles cans were falling over.  The solution was some microwave pots that tapered from the bottom to the top turned upside down and with a hole cut int he bottom.  This gives a nice wide base. If further stability is needed these pots could be filled with sand or stones to weigh down the bottom even more.
microwave pot used as base

This was an interesting build as I never used NRF24L01s before.  Finding the information on how to get them to work and wire them up was interesting as most of the guides nearly provided the full details and they all seemed to provide slightly different information. So, I enjoyed solving that riddle and end up with a playable game that scales in the number of buttons and also the space.
This could be set up as 10 buttons on a table with all the cans tied to each other or as 20 buttons around the outside of a field.  You'll hear in the video below that the Raspberry Pi can call out the button numbers. This definitely made it a bit easier as you quickly learn which button is which position.  For outdoor use if it is very sunny I have a feeling the LEDs in the buttons will not be bright enough.  So, next I will be adding more lights and indicators to the buttons so they can be identified outdoors.


Here's another video of the game being played at the Cambridge Raspberry Jam.


If you have any questions or comments let me know.





Thursday, November 12, 2015

Codebug - Flashing LED tree made with conductive paint

Last year I made some conductive paint using this Instructable.  For me 50:25 by weight, paint to graphite powder works great. Make sure to stir in slowly otherwise the graphite powder will fly.
With this mix the paint is easy to apply yet has good conductivity.
It worked great and I used it at a Raspberry Jam to make flashing LED eyes for Halloween.

Since then the paint has been abandoned until my eldest daughter mentioned she was making circuits in school yesterday and I said I'd try to make them flashing LED Christmas Tree pictures.
This got a thumbs up from my daughters so the pressure was on.

Using Inkscape I drew a simple Christmas Tree with a place to put 7 LEDs.
Nice and simple and easy to print on A4 card.  I made the baubles the shape of a physical LED so you can see which way to place the LEDs on the sheet. All the negative (ground, cathode) sides in the middle making it easier to connect all to GND later.
PNG Version.
Links to PDF and SVG 

With the drawing done I added the LEDs.  6 to the green tips and a yellow one in the star.  I had 2mm LEDs to hand so that's what I used.  For the real thing I plan to use 5mm as the effect should be better.

I found pre-bending the LED leads at right angles so they are pointing down when in places made it easier to  get the LED to stand up at the front.  I also made small holes using a pin to push the LED leads through.  It's hard to push LEDs through 300gsm card as the leads don't have a point.

Once this was done I turned over the card.  Left the ground side for each LED pointing down and turned the positive side (anode) out at right angles pointing to the edge.  Once done I used a small piece of tape to keep the LEDs in place.
Tip.  Push the leads down to they push against the card. It's means the leads lie flat making them easier to hold on with just paint.

Back of the card
As I mentioned the conductive paint had spent the last year drying so was a bit sticky meaning the paint job is less than smooth.  I have since revived the paint with a little bit of water so for the kids it should be much easier to paint.

The bar down the middle is the ground tied to all the LEDs.  The other lines are for the positive side of each LED.
If you want all wires to be at the bottom then you may have to adjust the SVG in Inkscape to only print one Tree per A4 landscape card.

To test I thought I'd use a Codebug I got in the Kickstarter and hardly used it since. They are no on sale at CPC Farnell
With the help of Twitter people (I didn't declare the legs as outputs at the top of my code) I put together the following program. Link to CodeBug site for project

CodeBug code
Once all this was done the LEDs flashed. Yeah!.

For this project I wired up the LEDs so the top and bottom on one side are paired with the middle one on the other side and vice versa and the star LED on a leg of it's own.
This only uses three  (3) legs on the CodeBug,s o the 4th one (3) in the code is redundant.  I just left it in there in case I can figure out a more exciting way to use 4 outputs to flash 7 LEDs.

Very Short video of the final tree.


Next I want to wire up to a Raspberry Pi to enable interactive input and independent control of all the LEDs and maybe play a Christmas tune as well.

Sidenote; Some might be asking where the current limiting resistor is.  It's the paint.  The conductive paint has a higher resistance than wire and so it is acting like the current limiting resistor.






Saturday, August 25, 2012

Arduino, Ethernet, Ethercard, XAMPP web server, PHP web page controlling 2 LEDs

[UPDATE 17 Sept 2012 - I have modified the XAMPP webserver code  get the details for the Arduino(s) from a database.  See the updated version here

I tinkered a while back with using a web server to control and Arduino over a serial connection and then I got an ENC28J60.  A really cheap Ethernet socket that can work with the Arduino. It uses the Ethercard community provided library.

My goal was to find a way to turn on and off LEDs which can later become Servos once I've done the hard coding from a web server that then connects to the Arduino over Ethernet.

Using this method the web server and the Arduino don't have to be near each other and even more importantly they don't need to be physically connected.

After a bit of research and trial and error I have something working.
This example presents a simple PHP based web page on the web server with the options to turn on or off 2 LEDs. (sorry for the gaudy colours in the video I thought it would be good to match the LED colours with the menus.

When you click a button on the web page it calls itself with a POST variable set.
The PHP page then interprets this and does an fopen() to the Arduino to do the requested action.

By doing an fopen() the actual connection to the Arduino is not shown on the web page.

The fun with this is that it could be expanded to control more pins easily and also control more than 1 Arduino.



PHP Code on the Server


<!--
Winlkeink
August 2012
For feedback, comments and questions go to winkleink.blogspot.com

This code works with the Ethercard_LED_ONOFF_PHPCall code for Arduino to control Pin2 and Pin4 on
the Arduino using a web server as the interface.
The web page is served with the options and when you click the button it does an fopen in the background
to the Arduino with the relevant command.
By doing this the IP address of the Arduino is not needed and also the commands to control the Arduino is not 
shown publicly when compared to using the Arduino as the web server itself and using a GET REQUEST.

By doing it this way you have more control over the Arduino and the web interface.

NOTE:
As always my code is rough and is designed to get things done rather than beign perfect or pretty.
Use as you wish and let me know your thoughts.

-->

<!-- Start of the HTML -->
<html>
<head>
<title>Click to Turn on or OFF the LED in the background</title>
</head>
<body bgcolor="#FF9933">
<?php

// Check of LED2 is set.  If it is use it
if (isset($_POST["LED2"]))
{
$LED2= $_POST["LED2"];
//echo "<b>$LED2</b>";
}
else
{
$LED2 ="";
}
if ($LED2 == "ON")
{
// Set led2 ON by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h = @fopen("http://192.168.1.5/?LED2=ON", "rb");
}
else if ($LED2 == "OFF")
{
// Set led2 OFF by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h= @fopen("http://192.168.1.5/?LED2=OFF", "rb");
}

// Check of LED4 is set.  If it is use it
if (isset($_POST["LED4"]))
{
$LED4= $_POST["LED4"];
//echo "<b>LED4 is $LED4</b>";
}
else
{
$LED4 ="";
}
if ($LED4 == "ON")
{
// Set led4 ON by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h = @fopen("http://192.168.1.5/?LED4=ON", "rb");
}
else if ($LED4 == "OFF")
{
// Set led4 OFF by calling the Arduino using fopen
//ini_set("allow_url_fopen On", true);
$h= @fopen("http://192.168.1.5/?LED4=OFF", "rb");
}

?>
<!-- LED2 FORM -->
<table>
<tr><td colspan="2"><font size="4" color="yellow">Turn on and off the LED2</font></H4></td></tr>
<tr><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED2" value="ON">
<input type="submit" name="submit" value="ON">
</form>
</td><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED2" value="OFF">
<input type="submit" name="submit" value="OFF">
</form>
</td></tr>
</table>

<table>
<tr><td colspan="2"><font size="4" color="green">Turn on and off the LED4</font></td></tr>
<tr><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED4" value="ON">
<input type="submit" name="submit" value="ON">
</form>
</td><td>
<form action="led2.php" method="post">
<input type="hidden" name="LED4" value="OFF">
<input type="submit" name="submit" value="OFF">
</form>
</td></tr>
</table>


</body>
</html>

Arduino code

/*
Winlkeink
August 2012
For feedback, comments and questions go to winkleink.blogspot.com

Script to allow the controling on the Arduino over Ethernet using an ENC28J60 Ethernet socket and the Ethercard library
Assigning Static IP for the Arduino so I can know exactly which Arduino I am controlling
For this example the request is either http://192.168.1.5/?LED2=ON or http://192.168.1.5/?LED2=OFF
These can be called directly but then the Arduino would have to be the web server and present back the web page
with the option to turn the LED off or ON

For this example I am controlling the Arduino from a webserver on my PC (XAMP) using a PHP script.

*/
// I took took inspiration from the following 2 examples

// The BackSoon example provided with the EtherCard library
// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

// Example from the Internet
// https://github.com/lucadentella/enc28j60_tutorial/blob/master/_5_BasicServer/_5_BasicServer.ino
#include <EtherCard.h>

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,1,5 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

// Using a Variable for the Pin, but it is not necessary 
const int ledPin2 = 2;
const int ledPin4 = 4;


// Some stuff for responding to the request
char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;

// Small web page to return so the request is completed
char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Arduino 192.168.1.5"
  "</title></head>"
  "<body>"
    "<h3>Arduino 192.168.1.5</h3>"
  "</body>"
"</html>"
;

void setup(){
// Set Pin2 to be an Output
  pinMode(ledPin2, OUTPUT);
// Set Pin4 to be an Output
  pinMode(ledPin4, OUTPUT);

// Scary complex intializing of the EtherCard - I don't understand this stuff (yet0  
  ether.begin(sizeof Ethernet::buffer, mymac);
// Set IP using Static
  ether.staticSetup(myip, gwip);
}

void loop(){
  
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

// IF LED2=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin2, HIGH);
    }

// IF LED2=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin2, LOW);
    }

// IF LED4=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?LED4=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin4, HIGH);
    }

// IF LED4=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED4=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin4, LOW);
    }

//Return a page so the request is completed.

    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  
}

If you want more details on using the ENC28J60 check out my previous post on it.  It gives details on wiring and various libraries for it.







Thursday, July 14, 2011

Arduino - Web server controlled LED using serial connection between the Arduino and web server

I was chatting to a friend who was working on home automation.  Controlling lights, heating and other stuff around the house using a touch screen controller and I thought if you used a web site as the interface then connected to the Arduino over a serial link it would be possible to switch things on and off from any devlice that could display a web page and access the web server.

Personally, I would not recommend using a serial link from the server to the Arduino as the Arduino resets itself everytime something is sent over serial,  More correctly an ethernet shield would be great.  Giving more scalability as the web server can talk to mutliple Arduinos with no great extra effort.

I installed XAMPP on my netbook as the webserver and used PHP code on the server to present the web interface and send the command to the Arduino over serial.  The Arduino is waiting for a serial command and then depending on what it is will turn on or off the mounted LED.

So, not the most impressive looking demo, but as a proof of concept if I can turn on a LED I can control a relay to turn on an kind of device.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


PHP on the server:

<?php

exec('mode com4: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";

/* Serial script for pan/tilt Camera with servos */
/* Script by Aneal Khimani, 2-12-10 */

//check the GET action SuperGlobal var to see if an
//action is to be performed

if (isset($_GET['action'])) {

$switch1 = $_GET['action'];

//Action required

switch ($switch1) {
case "on":
$fp = fopen("com4", "w");
fwrite($fp, chr(97));
fclose($fp);
break;

case "off":
$fp = fopen("com4", "w");
fwrite($fp, chr(98));
fclose($fp);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a></td><td><?php if($switch1 == 'on'){echo "<b>ON</b>";} ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a></td><td><?php if($switch1 == 'off'){echo "<b>OFF</b>";} ?></td></tr>
</table>
</p>

</center>
</body>
</html>


Arduino Code:

/*

  Used with the PHP code
  http://localhost/arduino.php
 */

void setup() {               

  Serial.begin(9600); // initialize serial communication:

  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {

// read the serial port:

if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte) {
case 'a':
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println("HIGH");
 
break;

case 'b':
  digitalWrite(13, LOW);   // set the LED off
  Serial.
  println("LOW");

break;
}

}

}

Monday, May 16, 2011

8x8 LED Matrix Scrolling Message Changing In Serial Monitor

The classic scrolling message with the ability to enter a message from the serial monitor and while it is scrolling if you enter a lowercase 'a' in the monitor it stops and you can enter a new message.

To do this I created my own character set (reminds me of the olden days with my Commodore 64 - yes I am that old)

It took 3 evenings to create the character set and then 2 evening to create the code.  So, if you are looking to do something similar the code below with the character set defined may save you some time.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.

// Control an 8x8 LED display with 2 x 74HC595 shift registers
// Using only 3 pins from the Arduino

// while condition variable
int whileVar = 0;

//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;

//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;

//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;

uint8_t led[8];
uint8_t letters[672];
uint8_t currentdisplay[8];

long counter1 = 0;

// Current Character in the charMessage Array
int charMessageCurrent = 0;

// Current line in Letter
int lineLetter = 672;

//currentChar is the current character in the charMessage that is being chekced
char currentChar =32;

// Used to store the message instead of displayMessage
char charMessage[40];

// scrollMessage is array holding the message that will be displayed
uint8_t scrollMessage[480];

// Serial read Byte
int incomingByte = 0;

void setup() {

Serial.begin(9600);
 
// Seed Random Generator with noise from analog pin 0 
randomSeed(analogRead(0));
 
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

// Symbol [ ] space
letters[0] =  B00000000;
letters[1] =  B00000000;
letters[2] =  B00000000;
letters[3] =  B00000000;
letters[4] =  B00000000;
letters[5] =  B00000000;
letters[6] =  B00000000;
letters[7] =  B00000000;

// Symbol !
letters[8]  =  B00000000;
letters[9]  =  B00000000;
letters[10] =  B00000000;
letters[11] =  B00000000;
letters[12] =  B11110011;
letters[13] =  B00000000;
letters[14] =  B00000000;
letters[15] =  B00000000;

// Symbol "
letters[16] =  B00000000;
letters[17] =  B00000000;
letters[18] =  B00000000;
letters[19] =  B11100000;
letters[20] =  B00000000;
letters[21] =  B11100000;
letters[22] =  B00000000;
letters[23] =  B00000000;

// Symbol #
letters[24] =  B00000000;
letters[25] =  B00100100;
letters[26] =  B11111111;
letters[27] =  B00100100;
letters[28] =  B00100100;
letters[29] =  B00100100;
letters[30] =  B11111111;
letters[31] =  B00100100;

// Symbol $
letters[32] =  B00000000;
letters[33] =  B01001110;
letters[34] =  B10010001;
letters[35] =  B10010001;
letters[36] =  B11111111;
letters[37] =  B10010001;
letters[38] =  B10010001;
letters[39] =  B01100110;

// Symbol %
letters[40] =  B00000000;
letters[41] =  B10000111;
letters[42] =  B01000101;
letters[43] =  B00110111;
letters[44] =  B00011000;
letters[45] =  B11100100;
letters[46] =  B10000010;
letters[47] =  B11100001;

// Symbol &
letters[48] =  B00000000;
letters[49] =  B01100000;
letters[50] =  B10010101;
letters[51] =  B10010011;
letters[52] =  B10010101;
letters[53] =  B10011001;
letters[54] =  B01011001;
letters[55] =  B00111110;

// Symbol '
letters[56] =  B00000000;
letters[57] =  B00000000;
letters[58] =  B00000000;
letters[59] =  B11100000;
letters[60] =  B00000000;
letters[61] =  B00000000;
letters[62] =  B00000000;
letters[63] =  B00000000;

// Symbol (
letters[64] =  B00000000;
letters[65] =  B00000000;
letters[66] =  B10000001;
letters[67] =  B01000010;
letters[68] =  B00100100;
letters[69] =  B00011000;
letters[70] =  B00000000;
letters[71] =  B00000000;

// Symbol )
letters[72] =  B00000000;
letters[73] =  B00000000;
letters[74] =  B00011000;
letters[75] =  B00100100;
letters[76] =  B01000010;
letters[77] =  B10000001;
letters[78] =  B00000000;
letters[79] =  B00000000;

// Symbol *
letters[80] =  B00000000;
letters[81] =  B10010010;
letters[82] =  B01010100;
letters[83] =  B00111000;
letters[84] =  B11111111;
letters[85] =  B00111000;
letters[86] =  B01010100;
letters[87] =  B10010010;

// Symbol +
letters[88] =  B00000000;
letters[89] =  B00010000;
letters[90] =  B00010000;
letters[91] =  B00010000;
letters[92] =  B11111111;
letters[93] =  B00010000;
letters[94] =  B00010000;
letters[95] =  B00010000;

// Symbol ,
letters[96]  =  B00000000;
letters[97]  =  B00000000;
letters[98]  =  B00000000;
letters[99]  =  B00000110;
letters[100] =  B00000001;
letters[101] =  B00000000;
letters[102] =  B00000000;
letters[103] =  B00000000;

// Symbol -
letters[104] =  B00000000;
letters[105] =  B00000000;
letters[106] =  B00000000;
letters[107] =  B00010000;
letters[108] =  B00010000;
letters[109] =  B00010000;
letters[110] =  B00000000;
letters[111] =  B00000000;

// Symbol .
letters[112] =  B00000000;
letters[113] =  B00000000;
letters[114] =  B00000000;
letters[115] =  B00000011;
letters[116] =  B00000011;
letters[117] =  B00000000;
letters[118] =  B00000000;
letters[119] =  B00000000;

// Symbol /
letters[120] =  B00000000;
letters[121] =  B10000000;
letters[122] =  B01000000;
letters[123] =  B00100000;
letters[123] =  B00011000;
letters[125] =  B00000100;
letters[126] =  B00000010;
letters[127] =  B00000001;

// Number 0 - zero
letters[128] =  B00000000;
letters[129] =  B00111100;
letters[130] =  B01000010;
letters[131] =  B10100001;
letters[132] =  B10010001;
letters[133] =  B10001001;
letters[134] =  B01000010;
letters[135] =  B00111100;

// Number 1
letters[136] =  B00000000;
letters[137] =  B00000000;
letters[138] =  B00000001;
letters[139] =  B11111111;
letters[140] =  B01000001;
letters[141] =  B00100001;
letters[142] =  B00000000;
letters[143] =  B00000000;

// Number 2
letters[144] =  B00000000;
letters[145] =  B01100001;
letters[146] =  B10010001;
letters[147] =  B10001001;
letters[148] =  B10001001;
letters[149] =  B10000101;
letters[150] =  B10000011;
letters[141] =  B01100001;

// Number 3
letters[152] =  B00000000;
letters[153] =  B01111110;
letters[154] =  B10011001;
letters[155] =  B10011001;
letters[156] =  B10011001;
letters[157] =  B10000001;
letters[158] =  B10000001;
letters[159] =  B01000110;

// Number 4
letters[160] =  B00000000;
letters[161] =  B00000100;
letters[162] =  B11111111;
letters[163] =  B01000100;
letters[164] =  B00100100;
letters[165] =  B00010100;
letters[166] =  B00001100;
letters[167] =  B00000100;

// Number 5
letters[168] =  B00000000;
letters[169] =  B10001110;
letters[170] =  B10010001;
letters[171] =  B10010001;
letters[172] =  B10010001;
letters[173] =  B10010001;
letters[174] =  B10010001;
letters[175] =  B11100010;

// Number 6
letters[176] =  B00000000;
letters[177] =  B01001110;
letters[178] =  B10010001;
letters[179] =  B10010001;
letters[180] =  B10010001;
letters[181] =  B10010001;
letters[182] =  B10010001;
letters[183] =  B01111110;

// Number 7
letters[184] =  B00000000;
letters[185] =  B11100000;
letters[186] =  B10010000;
letters[187] =  B10001000;
letters[188] =  B10000111;
letters[189] =  B00000000;
letters[190] =  B00000000;
letters[191] =  B00000000;

// Number 8
letters[192] =  B00000000;
letters[193] =  B01100110;
letters[194] =  B10011001;
letters[195] =  B10011001;
letters[196] =  B10011001;
letters[197] =  B10011001;
letters[198] =  B10011001;
letters[199] =  B01100110;

// Number 9
letters[200] =  B00000000;
letters[201] =  B01111110;
letters[202] =  B10001001;
letters[203] =  B10001001;
letters[204] =  B10001001;
letters[205] =  B10001001;
letters[206] =  B10001001;
letters[207] =  B01110010;

// Symbol :
letters[208] =  B00000000;
letters[209] =  B00000000;
letters[210] =  B00000000;
letters[211] =  B00000000;
letters[212] =  B01100110;
letters[213] =  B00000000;
letters[214] =  B00000000;
letters[215] =  B00000000;

// Symbol ;
letters[216] =  B00000000;
letters[217] =  B00000000;
letters[218] =  B00000000;
letters[219] =  B00000000;
letters[220] =  B01100110;
letters[221] =  B00000001;
letters[222] =  B00000000;
letters[223] =  B00000000;

// Symbol <
letters[224] =  B00000000;
letters[225] =  B00000000;
letters[226] =  B00000000;
letters[227] =  B10000010;
letters[228] =  B01000100;
letters[229] =  B00101000;
letters[230] =  B00010000;
letters[231] =  B00000000;

// Symbol =
letters[232] =  B00000000;
letters[233] =  B00000000;
letters[234] =  B00000000;
letters[235] =  B00100100;
letters[236] =  B00100100;
letters[237] =  B00100100;
letters[238] =  B00100100;
letters[239] =  B00000000;

// Symbol >
letters[240] =  B00000000;
letters[241] =  B00000000;
letters[242] =  B00010000;
letters[243] =  B00101000;
letters[244] =  B01000100;
letters[245] =  B10000010;
letters[246] =  B00000000;
letters[247] =  B00000000;

// Symbol ?
letters[248] =  B00000000;
letters[249] =  B00000000;
letters[250] =  B01100000;
letters[251] =  B10010000;
letters[252] =  B10001101;
letters[253] =  B10000000;
letters[254] =  B01100000;
letters[255] =  B00000000;

// Symbol @
letters[256] =  B00000000;
letters[257] =  B01111000;
letters[258] =  B10100101;
letters[259] =  B10100101;
letters[260] =  B10100101;
letters[261] =  B10011001;
letters[262] =  B10000001;
letters[263] =  B01011110;

// Letter A
letters[264] = B00000000;
letters[265] = B00111111;
letters[266] = B01001000;
letters[267] = B10001000;
letters[268] = B10001000;
letters[269] = B10001000;
letters[270] = B01001000;
letters[271] = B00111111;

// Letter B
letters[272]  = B00000000;
letters[273]  = B01110110;
letters[274] = B10001001;
letters[275] = B10001001;
letters[276] = B10001001;
letters[277] = B10001001;
letters[278] = B10001001;
letters[279] = B11111111;

// Letter C
letters[280] = B00000000;
letters[281] = B00100100;
letters[282] = B01000010;
letters[283] = B10000001;
letters[284] = B10000001;
letters[285] = B10000001;
letters[286] = B01000010;
letters[287] = B00111100;

// Letter D
letters[288] = B00000000;
letters[289] = B00111100;
letters[290] = B01000010;
letters[291] = B10000001;
letters[292] = B10000001;
letters[293] = B10000001;
letters[294] = B10000001;
letters[295] = B11111111;

// Letter E
letters[296] = B00000000;
letters[297] = B10000001;
letters[298] = B10000001;
letters[299] = B10010001;
letters[300] = B10010001;
letters[301] = B10010001;
letters[302] = B10010001;
letters[303] = B11111111;


// Letter F
letters[304] = B00000000;
letters[305] = B10000000;
letters[306] = B10000000;
letters[307] = B10010000;
letters[308] = B10010000;
letters[309] = B10010000;
letters[310] = B10010000;
letters[311] = B11111111;

// Letter G
letters[312] = B00000000;
letters[313] = B00101100;
letters[314] = B01001010;
letters[315] = B10001001;
letters[316] = B10000001;
letters[317] = B10000001;
letters[318] = B01000010;
letters[319] = B00111100;

// Letter H
letters[320] = B00000000;
letters[321] = B11111111;
letters[322] = B00001000;
letters[323] = B00001000;
letters[324] = B00001000;
letters[325] = B00001000;
letters[326] = B00001000;
letters[327] = B11111111;

// Letter I
letters[328] = B00000000;
letters[329] = B00000000;
letters[330] = B10000001;
letters[331] = B10000001;
letters[332] = B11111111;
letters[333] = B10000001;
letters[334] = B10000001;
letters[335] = B00000000;

// Letter J
letters[336] = B00000000;
letters[337] = B10000000;
letters[338] = B10000000;
letters[339] = B11111100;
letters[340] = B10000010;
letters[341] = B10000001;
letters[342] = B10000001;
letters[343] = B10000010;

// Letter K
letters[344] = B00000000;
letters[345] = B10000001;
letters[346] = B01000010;
letters[347] = B00100100;
letters[348] = B00011000;
letters[349] = B00001000;
letters[350] = B00000100;
letters[351] = B11111111;

// Letter L
letters[352] = B00000000;
letters[353] = B00000001;
letters[354] = B00000001;
letters[355] = B00000001;
letters[356] = B00000001;
letters[357] = B00000001;
letters[358] = B00000001;
letters[359] = B11111111;

// Letter M
letters[360] =  B00000000;
letters[361] =  B01111111;
letters[362] =  B10000000;
letters[363] =  B10000000;
letters[364] = B01110000;
letters[365] = B10000000;
letters[366] = B10000000;
letters[367] = B01111111;

// Letter N
letters[368] =  B00000000;
letters[369] =  B11111111;
letters[370] =  B00000010;
letters[371] =  B00000100;
letters[372] =  B00011000;
letters[373] =  B00100000;
letters[374] =  B01000000;
letters[375] =  B11111111;

// Letter 0
letters[376] =  B00000000;
letters[377] =  B00111100;
letters[378] =  B01000010;
letters[379] =  B10000001;
letters[380] =  B10000001;
letters[381] =  B10000001;
letters[382] =  B01000010;
letters[383] =  B00111100;

// Letter P
letters[384] =  B00000000;
letters[385] =  B00110000;
letters[386] =  B01001000;
letters[387] =  B10000100;
letters[388] =  B10000100;
letters[389] =  B10000100;
letters[390] =  B10000100;
letters[391] =  B11111111;

// Letter Q
letters[392] =  B00000000;
letters[393] =  B00111101;
letters[394] =  B01000010;
letters[395] =  B10000101;
letters[396] =  B10001001;
letters[397] =  B10000001;
letters[398] =  B01000010;
letters[399] =  B00111100;

// Letter R
letters[400] =  B00000000;
letters[401] =  B00110001;
letters[402] =  B01001010;
letters[403] =  B10000100;
letters[404] =  B10000100;
letters[405] =  B10000100;
letters[406] =  B10000100;
letters[407] =  B11111111;

// Letter S
letters[408] =  B00000000;
letters[409] =  B01001110;
letters[410] =  B10010001;
letters[411] =  B10010001;
letters[412] =  B10010001;
letters[413] =  B10010001;
letters[414] =  B10010001;
letters[415] =  B01100110;

// Letter T
letters[416] =  B00000000;
letters[417] =  B10000000;
letters[418] =  B10000000;
letters[419] =  B10000000;
letters[420] =  B11111111;
letters[421] =  B10000000;
letters[422] =  B10000000;
letters[423] =  B10000000;

// Letter U
letters[424] =  B00000000;
letters[425] =  B11111100;
letters[426] =  B00000010;
letters[427] =  B00000001;
letters[428] =  B00000001;
letters[429] =  B00000001;
letters[430] =  B00000010;
letters[431] =  B11111100;

// Letter V
letters[432] =  B00000000;
letters[433] =  B11111000;
letters[434] =  B00000100;
letters[435] =  B00000010;
letters[436] =  B00000001;
letters[437] =  B00000010;
letters[438] =  B00000100;
letters[439] =  B11111000;

// Letter W
letters[440] =  B00000000;
letters[441] =  B11111110;
letters[442] =  B00000001;
letters[443] =  B00000001;
letters[444] =  B00001110;
letters[445] =  B00000001;
letters[446] =  B00000001;
letters[447] =  B11111110;

// Letter X
letters[448] =  B00000000;
letters[449] =  B10000001;
letters[450] =  B01000010;
letters[451] =  B00100100;
letters[452] =  B00011000;
letters[453] =  B00100100;
letters[454] =  B01000010;
letters[455] =  B10000001;

// Letter Y
letters[456] =  B00000000;
letters[457] =  B10000000;
letters[458] =  B01000000;
letters[459] =  B00100000;
letters[460] =  B00011111;
letters[461] =  B00100000;
letters[462] =  B01000000;
letters[463] =  B10000000;

// Letter Z
letters[464] =  B00000000;
letters[465] =  B10000001;
letters[466] =  B11000001;
letters[467] =  B10100001;
letters[468] =  B10010001;
letters[469] =  B10001001;
letters[470] =  B10000101;
letters[471] =  B10000011;

// Symbol !
letters[472] =  B00000000;
letters[473] =  B00000000;
letters[474] =  B00000000;
letters[475] =  B00000000;
letters[476] =  B11110011;
letters[477] =  B00000000;
letters[478] =  B00000000;
letters[479] =  B00000000;

// Symbol "
letters[480] =  B00000000;
letters[481] =  B00000000;
letters[482] =  B00000000;
letters[483] =  B11100000;
letters[484] =  B00000000;
letters[485] =  B11100000;
letters[486] =  B00000000;
letters[487] =  B00000000;

// Symbol #
letters[488] =  B00000000;
letters[489] =  B00100100;
letters[490] =  B11111111;
letters[491] =  B00100100;
letters[492] =  B00100100;
letters[493] =  B00100100;
letters[494] =  B11111111;
letters[495] =  B00100100;

// Symbol $
letters[496] =  B00000000;
letters[497] =  B01001110;
letters[498] =  B10010001;
letters[499] =  B10010001;
letters[500] =  B11111111;
letters[501] =  B10010001;
letters[502] =  B10010001;
letters[503] =  B01100110;

// Symbol %
letters[504] =  B00000000;
letters[505] =  B00000000;
letters[506] =  B01000110;
letters[507] =  B00110000;
letters[508] =  B00011000;
letters[509] =  B00000100;
letters[510] =  B01100010;
letters[511] =  B00000001;

// Symbol &
letters[512] =  B00000000;
letters[513] =  B00000000;
letters[514] =  B00000101;
letters[515] =  B01000010;
letters[516] =  B10100101;
letters[517] =  B10101001;
letters[518] =  B01010001;
letters[519] =  B00101110;

// Symbol '
letters[520] =  B00000000;
letters[521] =  B00000000;
letters[522] =  B00000000;
letters[523] =  B11100000;
letters[524] =  B00000000;
letters[525] =  B00000000;
letters[526] =  B00000000;
letters[527] =  B00000000;

// Symbol (
letters[528] =  B00000000;
letters[529] =  B00000000;
letters[530] =  B10000001;
letters[531] =  B01000010;
letters[532] =  B00100100;
letters[533] =  B00011000;
letters[534] =  B00000000;
letters[535] =  B00000000;

// Symbol )
letters[536] =  B00000000;
letters[537] =  B00000000;
letters[538] =  B00011000;
letters[539] =  B00100100;
letters[540] =  B01000010;
letters[541] =  B10000001;
letters[542] =  B00000000;
letters[543] =  B00000000;

// Symbol *
letters[544] =  B00000000;
letters[545] =  B10010010;
letters[546] =  B01010100;
letters[547] =  B00111000;
letters[548] =  B11111111;
letters[549] =  B00111000;
letters[550] =  B01010100;
letters[551] =  B10010010;

// Symbol +
letters[552] =  B00000000;
letters[553] =  B00010000;
letters[554] =  B00010000;
letters[555] =  B00010000;
letters[556] =  B11111111;
letters[557] =  B00010000;
letters[558] =  B00010000;
letters[559] =  B00010000;

// Symbol '
letters[560] =  B00000000;
letters[561] =  B00000000;
letters[562] =  B00000000;
letters[563] =  B11000000;
letters[564] =  B00100000;
letters[565] =  B00000000;
letters[566] =  B00000000;
letters[567] =  B00000000;

// Symbol -
letters[568] =  B00000000;
letters[569] =  B00000000;
letters[570] =  B00000000;
letters[571] =  B00010000;
letters[572] =  B00010000;
letters[573] =  B00010000;
letters[574] =  B00000000;
letters[575] =  B00000000;

// Symbol .
letters[576] =  B00000000;
letters[577] =  B00000000;
letters[578] =  B00000000;
letters[579] =  B00000011;
letters[580] =  B00000011;
letters[581] =  B00000000;
letters[582] =  B00000000;
letters[583] =  B00000000;

// Symbol /
letters[584] =  B00000000;
letters[585] =  B10000000;
letters[586] =  B01000000;
letters[587] =  B00100000;
letters[588] =  B00011000;
letters[589] =  B00000100;
letters[590] =  B00000010;
letters[591] =  B00000001;

// Number 0 - zero
letters[592] =  B00000000;
letters[593] =  B00111100;
letters[594] =  B01000010;
letters[595] =  B10100001;
letters[596] =  B10010001;
letters[597] =  B10001001;
letters[598] =  B01000010;
letters[599] =  B00111100;

// Number 1
letters[600] =  B00000000;
letters[601] =  B00000000;
letters[602] =  B00000001;
letters[603] =  B11111111;
letters[604] =  B01000001;
letters[605] =  B00100001;
letters[606] =  B00000000;
letters[607] =  B00000000;

// Number 2
letters[608] =  B00000000;
letters[609] =  B01100001;
letters[610] =  B10010001;
letters[611] =  B10001001;
letters[612] =  B10001001;
letters[613] =  B10000101;
letters[614] =  B10000011;
letters[615] =  B01100001;

// Number 3
letters[616] =  B00000000;
letters[617] =  B01111110;
letters[618] =  B10011001;
letters[619] =  B10011001;
letters[620] =  B10011001;
letters[621] =  B10000001;
letters[622] =  B10000001;
letters[623] =  B01000110;

// Number 4
letters[624] =  B00000000;
letters[625] =  B00000100;
letters[626] =  B11111111;
letters[627] =  B01000100;
letters[628] =  B00100100;
letters[629] =  B00010100;
letters[630] =  B00001100;
letters[631] =  B00000100;

// Number 5
letters[632] =  B00000000;
letters[633] =  B10001110;
letters[634] =  B10010001;
letters[635] =  B10010001;
letters[636] =  B10010001;
letters[637] =  B10010001;
letters[638] =  B10010001;
letters[639] =  B11100010;

// Number 6
letters[640] =  B00000000;
letters[641] =  B01001110;
letters[642] =  B10010001;
letters[643] =  B10010001;
letters[644] =  B10010001;
letters[645] =  B10010001;
letters[646] =  B10010001;
letters[647] =  B01111110;

// Number 7
letters[648] =  B00000000;
letters[649] =  B11100000;
letters[650] =  B10010000;
letters[651] =  B10001000;
letters[652] =  B10000111;
letters[653] =  B00000000;
letters[654] =  B00000000;
letters[655] =  B00000000;

// Number 8
letters[656] =  B00000000;
letters[657] =  B01100110;
letters[658] =  B10011001;
letters[659] =  B10011001;
letters[660] =  B10011001;
letters[661] =  B10011001;
letters[662] =  B10011001;
letters[663] =  B01100110;

// Number 9
letters[664] =  B00000000;
letters[665] =  B01111110;
letters[666] =  B10001001;
letters[667] =  B10001001;
letters[668] =  B10001001;
letters[669] =  B10001001;
letters[670] =  B10001001;
letters[671] =  B01110010;


for (int i = 0; i < 8; i++){
  led[i] = letters[i];
  currentdisplay[i] = letters[i];
  }
}

void loop() {

// Clear the screen before starting
for (int i=448; i <456; i++){
  led[i-448] = letters[i];
  currentdisplay[i-448] = letters[i];;
}
screenUpdate();

// Reseting variables
whileVar = 0;
counter1 = 0;
charMessageCurrent = 0;
lineLetter = 672;
currentChar =32;

//  Start Reading the Serial Data

while (whileVar ==0){
screenUpdate();
 
// send data only when you receive data:
    if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

                // If the byte read is an 'a' stop, otherwise add the byte to the string
                if (incomingByte ==97){
                whileVar =1;
                }
                else
                {
                charMessage[charMessageCurrent] = incomingByte;
                charMessageCurrent++;
                } 
        }
}

// End Reading Serial Data 
 

// prints charMessage
Serial.write("charMessage is: ");

for (int i=0; i<charMessageCurrent; i++){
Serial.write(charMessage[i]);
}
Serial.println("");

// Making the first 8 Bytes - character a space
for(int i =0; i <8; i++){
  scrollMessage[i] = B00000000;
}
// Move counter1 to 8 so that the space stays in place
counter1=8; 
 
for (int i=0; i < charMessageCurrent; i++){
currentChar = charMessage[i];

for (int x=7; x >= 0; x--){
  scrollMessage[counter1] = letters[((currentChar-32)*8)+x];
  counter1++;
  }
}
// End Sorting out the message for scrolling

// Clear the screen before starting
for (int i=0; i <8 ; i++){
  led[i] = B00000000;
  currentdisplay[i] = B00000000;;
}
screenUpdate();

// Reseting current1 to 0 as it is used for the delay in the code below
counter1=0;

// Resetting whileVar so it can be used again
whileVar=0;

while (whileVar ==0)
{
// Check if I enter just an a - lower case a to exit the system
    if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

                // If the byte read is an 'a' stop, otherwise add the byte to the string
                if (incomingByte ==97){
                whileVar =1;
                }
        }  
// counter1 used for delay in animation
counter1++;

// set the LEDs
screenUpdate();

// Loop for the action - counter1 used for the delay in scrolling
if (counter1 >= 25) {
counter1 = 0;
lineLetter++;

if (lineLetter >(((charMessageCurrent)*8)+7)){
  lineLetter = 0;
  }
// Do scrolling

for (int i = 8; i > 0; i--){
  led[i] = currentdisplay[i-1];
  }
led[0] = scrollMessage[lineLetter];

for (int i=0; i <8; i++){
  currentdisplay[i] = led[i];
  }

}

}

}

void screenUpdate() {
uint8_t row = B00000001;

for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array

// Close the latch, sending the data in the registers out to the matrix
digitalWrite(latchPin, HIGH);
row = row << 1;
  }
}

void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock

boolean pinState;

//clear shift register read for sending data
digitalWrite(dataPin, LOW);

// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {

  //set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);

// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}
else {
  pinState = LOW;
}

//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);

//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}

//stop shifting
digitalWrite(clockPin, LOW);
}