This is reminder for me and hopefully useful for others either new to Arduino or using different boards.
Sometimes on uploading you can get an error message
avrdude: stk500_recv(): programmer is not responding avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
Of course the attempt number changes as it tries 10 times.
There are a few relatively obvious things that can help stop this error message.
Select the right board. If you're using an Uno, select Uno. If you're using a Leonardo, select Leonardo.
Select the correct processor: ATMega328 is the Uno one and the default
Make sure your USB port is selected.
Even after doing both of these and it all looking right you might still be getting the error message.
This is where we get into a discussion about Arduino compatible boards. The Arduino is an Open Source design and so it is perfectly legal and acceptable for anyone to copy the design and make their own boards and sell them (but they cannot call it an Arduino, topic for another day)
Some of these compatible boards use a different USB chip (CH340) and the ATMega328 could have a different bootloader to the actual Arduino.
if you are using a compatible board that has a CH340 make sure to install the driver for the chip otherwise you may not even be able to see the board in the IDE.
Then for the bootloader there is an alternative option under the Processor called "ATMega328P (Old Bootloader)" Not where I expect to see an alternative bootloader option but there it is.
ATMega328p (Old Bootloader)
This is the one I have to use for compatible boards.
So, once the correct board, processor, and port are selected you should be able to program your Arduino with ease.
Finally, finally. If you're running Linux and not Windows there is one more thing to take into account and that's permissions. By default a regular user account does not have permission to read/write to USB serial ports so you have to add your user to the group with access.
The command you need is as follows where [USERNAME] is replaced with your username.
sudo usermod -a -G dialout [USERNAME]
WARNING: This command is run using sudo and so has root access. It's always the right thing when finding commands online that use sudo to do your own research before blindly using the command. So, here's where I got the command from https://www.arduino.cc/en/Guide/Linux/
Hope you find this useful as I expect I'll be back here again when I set up my next computer or buy some new compatible boards.
I backed the pi-topCEED during the crowd funding campaign and have used it a lot since.
pi-topCEED
My kids also use it for homework and general Minecraft playing.
As I like to make things I have a number of microSD which means I regularly use it with Raspbian Jessie.
There are a couple of drawback to using Raspbian on the pi-topCEED. The pi-topHUB doesn't shutdown the power completely leaving the backlight on and the Raspberry Pi in kind of a suspecd mode. The red LED is still on. Also, there is no way to control the screen brightness.
Doing the usual Google to see if there was a way around this I came across this GitHub repository that looked like it would do the job. https://github.com/rricharz/pi-top-install
It includes code to shutdown the pi-topHUB which also completely shuts down power to the display and to the Pi as well as provide a program called 'brightness' to control the screen brightness.
After following the instructions in the README.md I had the pi-topHUB shutting down correctly and if from the terminal I type brightness increase the screen brightness would go up and vice versa brightness decrease did what you'd expect.
All very good, but not very user friendly if you're running the GUI which is where I spend most of my time.
With that in mind and having recently heard good thing about guizero (https://github.com/lawsie/guizero) which is based on tkinter and is said to make building GUI apps in Python easier.
The program itself is quite short. Code below. This makes 2 buttons. Button1 and Button2. When you press them they run the relevant system comment to change the brightness.
After a quick chmod +x pi-top-brightness-gui.py the program was executable and so I could run it directly.
Then I added the program to the main menu under the System Tools section.
In the video you can see I have a pi-top icon. This I took from their Twitter account (https://twitter.com/GetPiTop) I expect they'll be OK with that as I'm adding even more branding to my pi-topCEED.
It's great to have a little gui app to change the brightness of the screen and even better to learn a new skill with guizero.
One thing I didn't mention is that when I rebooted the Raspberry Pi the first time after installing the programs the screen brightness was far lower than I wanted it be by default.
To fix this I added a single line cronjob. Crontab with cronjobs is a way to schedule when programs run automatically with one of the options being to run at boot up. It's a simple way to get something running each time you login. It's the system used by people who lets say want to check their network connection every hour. Just setup a cronjob to run every hour and it will take care of it for you. I used this previously to enable the Bluetooth keyboard and trackpad or the NexDock. (http://www.winkleink.com/2016/10/nexdock-thoughts.html)
So, I used crontab to set the brightness of the screen to 8 on startup.
In the terminal type:
crontab -e
If this is your first time it asks which editor you want to use. I do [2] for nano.
Then the crontab file is opened where you put the jobs you want to run.
There are some comments in the file to help you get started, but if you want to do serious scheduled jobs then it's worth
Go to the bottom of the file and type
@reboot brightness 8
Then the usual nano [CTRL]-[x] to save and close. Answering [y] to save the file with the same name
Now when I start up the pi-topCEED the default brightness is '8' which suits me.
Finally, finally, the rricharz GitHub explains how to set the keyboard shortcuts for the pi-top laptop so the brightness keys work. All good if I had a pi-top laptop, but I have a pi-topCEED where it's bring your own keyboard and the one I'm using doesn't have any fancy keys.
So, I looked up how to add keyboard shortcuts. Again, it ends out to be not too hard. Just added a couple of entries into a config file.
First thing I had to do is decide what key combination I wanted to use. I settled on:
[Shift]-[Windows Key]-[Up Arrow] for brighter
[Shift]-[Windows Key]-[Down Arrow] for darker
To do this you need to edit the lxde-pi-rc.xml file.
The file is a in a hidden folder in your home folder. If using the default user pi then the following command will open it
nano /home/pi/.config/openbox/lxde-pi-rc.xml
As the file is in the home folder it doesn't need sudo to edit it
Then scroll down to the keyboard section and add the following to the end.
Easiest way is in nano find the </keyboard> text using the command [Ctrl]-[w] to do a Where is
Make sure you use spaces for the indent and get the <keybind> to line up with the other <keybind> entries
To finish like above use [Ctr]-[x] to save and close.
To test select [shutdown] in the drop down menu and then [logout].
No need to reboot to enable the keyboard shortcuts.
Once you log back in the lxde-pi-rc.xml setting will be re-enabled and the 2 new key bindings will be active giving you the ability to change the brightness of your pi-top/pi-topCEED using [Shift]-[Windows Key]-[Up Arrow] and [Shift]-[Windows key]-[Down Arrow]
This has been a fun little project to improve the use of the pi-topCEED.
I like the work rricharz has done getting the pi-topHUB to turn off completely and to control the brightness as well as Laura Sach has done to make guizero so easy to use.
With the help of other peoples effort I now have the pi-topCEED completely shutdown as well as the Raspberry Pi and the ability to change the screen brightness using either a little gui app or with the keyboard. Not a bad result for a couple of hours.
If you have any questions let me know in the comments.
I came across some videos of Jeff Minter who created some amazingly original games in the 8 bit era and is still going strong developing new games today and it got me thinking of one of his stranger games that I remember playing on the Commodore VIC-20.
Meta Galactic Llama Battle at the Edge of Time. Here's a video of it on the Commodore 64. I can't remember if I had it for the C64 as well, but definitely played it on the VIC-20
As with most of Jeff Minters games it kind of takes a slightly different approach.
You're a Llama at the bottom of the screen and you can move left and right. You can exit one side of the screen and come in the other side as well.
You shoot lasers out of your mouth at 45 degrees and you have to shoot the spiders falling from the top of the screen.
If a spider reaches the ground it starts to crawl towards you making evasive action essential.
All very interesting so far. To add to the complexity and the control you can also raise and lower a ceiling for your laser to bounce off of to assist in shooting the spiders.
I had great fun with this game all those years ago and so wanted to see if I could re-create something similar using Scratch.
After some fumbling I put created a little game inspired by the Llamas.
It's been fun developing a game in Scratch. I'm most definitely not an expert and I expect there are a lot of refinements that could be done to the code to improve the game but it plays similar to the original. I hope you enjoy it.
The final session of the robot club at my kids school was Easter break and I promised them a handout detailing resources that are free they can use to continue to learn to code. The kids were aged 10-12 and most were very new to coding so wanted to give a range of resources. They've spend the last term building small robots with an Arduino as the main controller so included Arduino as well as general programming resources.
As per the sheet a great combination for new people starting out if they don't have any hardware is the Earthshine Electronics guide and the AtuoDesk 123D Circuits online electronics and Arduino emulator. The guide gives 14 projects of varying degrees of complexity from an electronics and programming standpoint and the AutoDesk 123D Circuits gives you a free resource to do your coding. The electronics pieces is done on a breadboard and is very visual. Reminds me of Fritzing.
[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.
Last Christmas as part of my stocking fillers I got an HC-SR04 ultrasonic distance sensor.
Like most of my electronic bits it's a cheap generic device from ebay. It's a small sensor that is suppose to do the same thing as the Ping sensor but for less money.
I finally unwrapped it and found a library for Arduino IDE 1.0 at HERE. There is even some sample code to read the HC-SR04 and display the results on an LCD display.
Wiring is really simple
VCC - 5V
GND - GND
Trig - Trigger Pin you define in the code
Echo - Echo Pin defined in the code
Since I don't have an LCD display (yet) I modified the code to use the Serial Monitor as the output.
Code Below:
#include "Ultrasonic.h"
int TriggerP = 13; // Trigger Pin for Sensor int EchoP = 12; // Echo Pin for Sensor Ultrasonic ultrasonic(TriggerP,EchoP); void setup() { Serial.begin(9600); }
void loop() { Serial.print("Distance: "); Serial.print(ultrasonic.Ranging(CM)); // Get Range in Centimetres Serial.println(" Cm."); delay(1000); Serial.println("..."); // Next lines. }
This worked great up to about 16cm. But beyond that it started to give me some random numbers greater than 4000, so I'd expect not very robust.
I continued my search and found the following code that doesn't use any library, and this code even does the Serial Monitor as the output so no modification needed.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 12
#define echoPin 13
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
This code performed better, working out to about 24cm.
While doing my research on using the HC-SR04 I did find a post (somewhere...) stating that when powering from USB the 5V line can be a little bit off so this might be part of my problem.
But, using either method I have a sensor that is good up to 15cm which could be good enough for a small autonimous robot as if I'm within 15 cms I will need to look around for another direction.
NOTE: If you are using a version of the Arduino IDE before 1.0 then at the following LINK you can get the relevant library. It looks like it's the same as the 1.0 library and has similar performance.
The hunt is on to see if there is a way of getting a reading beyond 24cm that is reliable.
Loving the tinkering.
UPDATE:
I combined some of the code from the Library with the direct code and now it reliably(ish) without detailed testing will work to 70cm.
The modified code is below.
/* HC-SR04 Ping distance sensor] VCC to arduino 5v GND to arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 More info at: http://goo.gl/kJ8Gl */
After the little bits of coding and small videos that I have posted already I now have a working version of the classic game Columns working on an Arduino using the TVOut Lobrary to drive a composite video display.
I need to do a little cleaning up of the code, but if you want a copy just ask. Happy to share.
Here's the usual video showing it working.
As promised now that I've tidied up the cdoe a bit here it is.
Still working with the Arduino and the TVOut library.
Now I have 3 block falling instead of 1.
Starting to look more like the basics of a game. A bit more coding to do and maybe some buttons to enable the player to actually control something.
I was watching the video below and I spotted that the Benchmark BM9 is getting Prime Numbers.
This reminded me of a program I wrote in 1986 (20 I'm old years ago) to work out Primes on an Apple ][ in school.
So, I just had to fire up a BASIC interpreter (BASIC-256) to see if I could recreate it again.
Below is the code I did today with loads of REM comments to explain what I did.
REM Printing the Prime numbers up to 10,000 (maxcheck)
REM Winkleink - 2012
REM Just print the 1st 3 Primes
print 1
print 2
print 3
REM x is what we are checking set x to the next biggest odd number
x=5
REM number up to which we will check for primes
maxcheck = 10000
while x < maxcheck
REM Start the checking with 3 as 1 isn't valid and we are only checking odd number
i = 3
REM Set indicator if prime to 1 (means prime)
isprime =1
REM We only have to check odd numbers up to the Square Root of X as any number that will divide into X evenly will be made up of a number below SQR(x) and 1 above SQR(x)
while i <= int(sqr(x))
REM Check not Prime by seeing if there is a remainder if there is no remainder set isprime to 0
if x/i = int(x/i) then isprime=0
REM If not prime then set i to be above the while loop
if isprime = 0 then i= int(sqr(x))
REM Increment i by 2 as only need to divide by odd
i = i +2
endwhile
REM If isprime is still 1 then it is a prime number
if isprime=1 then print x
REM Increment x by 2,again even numbers won't be prime
x = x +2
endwhile
It was great fun to do and really brought back memories of sitting in the computer room in school watching as it slowly printed out the results. A bit faster today.
And here is a video of it working.
As always. Code is rough and just done for fun. If you know a better way to calculate primes let me know in the comments.
So at the end of my last adventure with the ENC28J60 and my Arduino Uno I got it working with the 0021 IDE (so I expect it will work with 0022 and 0023 but I have not tested) and the nuelectronics.com library available from here http://www.nuelectronics.com/download/projects/etherShield.zip using the following wiring to an Uno.
After my success with this I thought I'd try it with IDE 1.0 and it failed. The nuelectronics library for the ENC28J60 does not work (as of writing) with IDE 1.0.
After a little more hunting I found the ethercard library https://github.com/jcw/ethercard that works with IDE 1.0. I loaded up the backSoon web server sketch that even uses DHCP to get it's IP addess.
FAIL...
I was beaten and abandoned the effort until today.
Well, not really today as between Thursday and today I have been trawling the web to find anything that would help me and I found it. Not any official update or guide but a post in the forum for the ethercard with the statement:
a new optional 3rd arg to EtherCard.begin() lets you specify the chip select pin (8..10, default is 8)
Wait a second the EtherCard library uses Pin 8 by default for CS.
So, today I setup up the Arduino and ENC28J60 with the following wiring
Started IDE 1.0, loaded the backSoon Sketch.
Compiled and ran it.
Checked my routers list of DHCP clients and there was 1 for Arduino-DB witn an IP of 192.168..1.2
Well an IP address has been assigned
Fired up Firefox and went to 192.168.1.2
SUCCESS!!!!!
Now it's all about figuring out all the technical gubbins on the library so I can do my own stuff.
I want to be able to send data to a webserver and also read data from a webserver and do something based on the data.
Not asking too much but at the moment I have no idea how to make the most of the EtherCard library so a bit of Googling and testing required. This will keep me entertained through the cold winter months...
For Christmas I got a small ENC28J60 Ethernet module from eBay in my stocking.
All the advertisers stated it worked with Arduino so I thought let's have a go.
This is a small cheap, less capable Ethernet connection than the official Ethernet Shield.
The official shield has support built into the IDE but this board has code from very clever contributors.
Last night I rigged up the board using instruction from http://www.electrodragon.com/?p=1112 and it failed.
Not the best start. So, I tried a few other libaries and again no success. I added voltage buffers (74HC125) to do the voltage change from 3.3V on the ENC28J60 to 5V on the Arduino.
I asked a question at electrondragon and in fairness to him (I'm assuming him - never met them) he came back straight away with some helpful tips.
Still no luck.
Beaten, off to bed I went and then did a bit more digging today. The 1 thing I didn't try was using an older IDE. I took this opportunity to upgrade to Arduino IDE 1.0 where before I was using Arduino IDE 0021.
So, tonight was my second attempt.
I copied the libraries to Arduino IDE 0021 that I had been using. I uploaded the sketch for the web server.
Opened up Firefox and browsed to 192.168.1.25.
Success 1st time.
I uploaded the ping sketch. Success 1st time.
I then wired up an LED to Pin 4 and loaded the etherShield_web_switch sketch that allows you to control the LED from a web browser.
Again - success 1st time
If you do a search for Arduino ENC28J60 in Google it will come up with stuff about using voltage convertor to convert the boards 3.3V level to the Arduino's 5V level. At the same time it is mentioned that the Arduino can handle lower voltage (I think as low as 2V) as a HIGH, so based on the recommendation from electrondragon I just wired the ENC28J60 directly to the pins of the Arduino with no problems at all.
It worked and there was no smoke.
NOTE: As always this is FYI and your smoke and funny smells may be different.
The final hurdle I had to overcome was that the kit I have has male-male jumper wires and as you can see from the picture the ENC28J60 board has pins rather than sockets, so my wires would not work.
I considered buying some male-female wires but then figured out I could use an old HDD IDE cable to convert the pins on the ENC28J60 board to sockets for my jumper wires.
All worked really well.
Now I have the test sketches working I'll have to examine the code and figure out how to do some more interesting things now that the Arduino is connected to the network.
From searching eBay it looks like the Chinese are now manufacturing cheap W5100 based shields that also include a micr-SD slot like the offical Arduino Ethernet shield. More expensive than the ENC28J60 board but potentially more cost effective as from my readings the W5100 based boards are supported by the built in Ethernet library so no messing with 3rd party libraries that may be out of sync with the IDE (like my experience her) Also the W5100 by all accounts provides more of the Ethernet stack so uses less memory on the Arduino and more robust.
After messing with the ENC28J60 I expect I will get a cheap W5100 board for doing anything serious.
Finally, there is no video for this one as I'm just running the standard Examples so nothing unique or special and I thought showing my screen equivalent to the electrondragon screens would be a bit of a waste.
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.
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;
// 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);
}