Not running routines

The "Eleven" is our Uno-equivalent Arduino-compatible board, but with a number of improvements including prototyping area, a mini-USB connector, LEDs mounted near the edge, and the D13 LED isolated using a FET. [Product page]
Post Reply
GraemeSPa
Posts: 15
Joined: Tue Sep 24, 2013 11:58 am
Contact:

Not running routines

Post by GraemeSPa » Sun Nov 17, 2013 5:39 am

so i built a shift register controlled LED display - 2 x 595 shift registers driving 16 LEDs. I use the OE line to turn the plane on and off and pulse the MR line to reset both registers on start up. I'm using SPI to send the data rather than shiftOut function because its more manly and bit crunchy. I have written several display routines and individually - they all work. In the code following , there are two of these display routine shown. I want to run the first routine DROPS, then the second routine CHUNKS, then repeat the loop. My problem is - when I try to run both routines in the loop() , only the CHUNKS routine runs. if I comment ( // ) the CHUNKS routine out, DROPS works on its own. What am I doing wrong?

Code: Select all


// --------------------------------------------------------------------------
// Sketch using SPI to move bytes to registers and light 16 LEDs
// this works, but can't run routines separately. 
// start of all setup structure............................

#include <SPI.h>                    // add the SPI library
const int OEpin1 = 3;             // pin for registers Output Enable
const byte LATCH = 10;         //  pin for registers latch
                                           // MOSI pin 11 - data pin - no need to define.
const int resetpin = 7;          // pin for registers Master Reset
int UpperByte = 1;               // initial setting of byte for register 1 
int LowerByte = 0;               // initial setting of byte for register 2
int del01 = 25;                    // set delay 1 to 25mS
int del02 = 500;                  // set delay 2 to 500mS
byte runcount = 10;            // number of times to run each display routine

void setup () {
  pinMode(resetpin,OUTPUT); 
  pinMode(OEpin1,OUTPUT);
  digitalWrite(resetpin,LOW);   // force MR low to reset both registers
  delay(1); 		      // hold it a while
  digitalWrite(resetpin,HIGH);  // now set MR high 
  digitalWrite(OEpin1,LOW);    // enable register output
  Serial.begin(9600);               // enable the Serial port for serial monitoring
  SPI.begin ();   		    // start the SPI routine
   }  // end of all setup structure ---------------------------------


void lightLEDs ()
  {
   digitalWrite (LATCH, LOW);
   SPI.transfer (LowerByte);   // shovel data to LED 1-8
   SPI.transfer (UpperByte);   // shovel data to LED 9-16
   digitalWrite (LATCH, HIGH);
   delay(del01);
  }    // end of lightLEDS

 
void DROPS()  // display routine 1 – light each of the 16 LEDS in turn from top to bottom
 {
  for (int i =0; i < runcount; i++)      // run this routine 10 times
{
lightLEDs();	       // light the LED up	
UpperByte <<= 1;	       //	 shift upper byte left 
LowerByte <<= 1;        //     shift lower byte left

if (UpperByte > 128) {  // pass the "ball" to the lower register
UpperByte=0;
LowerByte=1;
 }
if (LowerByte > 128) { // if at the end, hop to the top
UpperByte=1;
LowerByte=0;
      }   
 }
 }  // end of DROPS
 
 
 
void CHUNKS()     // display routine 2 – light upper register 8 LEDs and flip between upper and  
// lower registers
 {
  for (int i =0; i < runcount; i++)  // run routine 10 times
{
UpperByte = 0xFF;   // to light entire top register
LowerByte = 0x00;   // to dark entire bottom register
lightLEDs();     // light them up
delay (del02);      // longer delay required

UpperByte = 0x00;  // dark top register
LowerByte = 0xFF;  // light bottom register
lightLEDs();   // light them up again
delay (del02);     // hold it 

}    // end of for function

}    // end of CHUNKS   

 void loop()
{
CHUNKS();       // this works
DROPS();        // this one doesn’t
}


andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Not running routines

Post by andrew » Sun Nov 17, 2013 9:15 pm

You haven't used SPI.setBitOrder() in your sketch to determine whether data goes out MSB or LSB first:
http://arduino.cc/en/Reference/SPISetBitOrder

GraemeSPa
Posts: 15
Joined: Tue Sep 24, 2013 11:58 am
Contact:

Re: Not running routines

Post by GraemeSPa » Tue Nov 19, 2013 3:37 am

I tried that SPI.setBitorder() when i got home from work with no change. I was getting sick of pulling my hair out and my missus is sick of her dining room table being used as a workbench so i bit a bullet and went back the the start and rewrote the lot using individual calls to display LEDS using shiftOut instead of a function call using SPI and took care to be sure the variables weren't tripping over each other. It worked and I can now call each display routine separately.
Woo Hoo! My smooth water fall of drops is a bit spiky though - the sketch clanks like an old Sherman tank but it works and I will now try to file off the rough edges and get the other display routines into the final sketch, get this project finished and the dining room table back to it's rightful owner. Mind you, i had a great idea for an LED dice project. And i need to finish my half built laser harp. I like these dweeno things.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Not running routines

Post by andrew » Tue Nov 19, 2013 4:14 am

Ah good to hear. If you like, feel free to demonstrate your projects in our "Project Showcase" sub-forum:
viewforum.php?f=6

GraemeSPa
Posts: 15
Joined: Tue Sep 24, 2013 11:58 am
Contact:

Re: Not running routines

Post by GraemeSPa » Tue Nov 19, 2013 4:33 am

thanks, i will do that.

Post Reply