Replicating the ShiftOut() function in java

I’m creating a 6x24 LED matrix using 74hc595 shift registers, and a decade counter. I’m using Processing 2.2.1 to control the components, so I need to recreate the shiftout() function in java. I’ve tested turning on individual leds, so I know the hardware works, but I’m having trouble with my recreation of the function. When I run this function, all the LEDs turn on and stay on. Can anyone help me figure out what is wrong?

Here is my code. The array bitArray is edited by some more code to set up the LEDs how i like, but that is irrelevant to the problem

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int clockPin = 13; 
int dataPin = 12; 
int latchPin = 11; 
int decadeClock = 9; 
int decadeReset = 8; 
int[][] bitArray = new int[6][24];
int[] ledPins = {8,9,11,12,13};
void setup(){
  
  arduino = new Arduino(this, Arduino.list()[0], 57600);


  bitArray[0][1] = 1; //solely to test a single LED

 for (int i = 0; i < ledPins.length; i++){
    arduino.pinMode(ledPins[i], Arduino.OUTPUT);
  }
}

void draw(){
  drawLed();
}

void drawLed(){
  arduino.digitalWrite(decadeReset, Arduino.LOW);
  arduino.digitalWrite(decadeReset, Arduino.HIGH);
  arduino.digitalWrite(latchPin, Arduino.LOW);

  for(int i = 0;i<6; i++) //loop for row scan
  {
    for(int x = 0; x<24; x++) //loop to write array data into collumns
    {
      if(bitArray[i][x] == 0)
      {
        arduino.digitalWrite(dataPin, Arduino.LOW);    
      }
      else
      {
        arduino.digitalWrite(dataPin, Arduino.HIGH);
      }
      arduino.digitalWrite(clockPin, Arduino.LOW); 
      arduino.digitalWrite(clockPin, Arduino.HIGH);
    }
      arduino.digitalWrite(decadeClock, Arduino.LOW); 
      arduino.digitalWrite(decadeClock, Arduino.HIGH); 
      
  }
   arduino.digitalWrite(latchPin, Arduino.HIGH);

  }

Thanks for the help in advance

Ugh - what a waste. Why not use the dedicated SPI hardware in the Arduino, and send the 3 bytes that make up the 24 bits from the PC?

Why not use the dedicated SPI hardware in the Arduino

Because Processing does not know how to.

Does processing know how to send out bytes of data? Send a byte over USB/Serial, let the arduino send it out via SPI.

Does processing know how to send out bytes of data? Send a byte over USB/Serial, let the arduino send it out via SPI.

Yes, it does. OP really should explain why he/she gave the Arduino a lobotomy.

Yes, it does. OP really should explain why he/she gave the Arduino a lobotomy

Not using Processing wasn't an option. I needed to use Processing to get what I wanted done (keyboard inputs), and I nearly completely left out the Arduino library mostly because I simply am not as good with it as I am with Java. I can try to rewrite the code so that Processing sends the array data to the serial monitor, and the Arduino takes that data and shifts it out, but I'd rather try to use the code I've already written.

The other problem is, shiftout only allows a byte to be shifted, and I have 3 shift registers shifted together. Is there a way to have it run shiftout 3 times at once? Or will it go fast enough that it isn't even noticed

shiftout on which end?

On the Arduino, SPI is very fast, shifting out at 4 megabits/second at default speed

digitalWrite (latchPin, LOW); SPI.transfer(byte0); SPI.transfer(byte1); SPI.transfer(byte2); digitalWrite (latchPin, HIGH);

or pull the data from an array SPI.transfer(incomingArray[0]); at the same speed.

Can set the SPI clock speed to 8 MHz too to go even faster.

On the Arduino, SPI is very fast, shifting out at 4 megabits/second at default speed

Is shiftOut() any slower? I've figured out how to make it work for my needs, and I don't really know how to use SPI at all, so if SPI doesn't present any advantage of shiftOut() i'll probably just stick with the latter.

Yes, SPI is significantly faster. It uses a dedicated shift register to clock data out at speeds of up to 8 MHz.
I presented the code for it above.
You add this at the top of the sketch:
#include <SPI.h>
and put this on void setup():
SPI.begin();

Then transfer per the code above:
digitalWrite (latchPin, LOW);
SPI.transfer(byte0);
SPI.transfer(byte1);
SPI.transfer(byte2);
digitalWrite (latchPin, HIGH);

shiftout on the other hand has to create all the clock edges in software.

  • determine the level of the next bit to go out
  • put the bit on the output pin
  • bring clock high
  • bring clock low
  • repeat

With SPI, you write the byte you want sent out into a dedicated register, and the hardware takes over. A byte can be transferred in as little as 17 clocks - nearly 1uS/byte.

How does SPI know which pin the shift register is hooked up to? Or do I need to set up the hardware differently in order for this to work? Currently, the clock, data, and latch are in pins 13, 12, and 11.

Thank you very much for your help, I really appreciate it.

How does SPI know which pin the shift register is hooked up to? Or do I need to set up the hardware differently in order for this to work? Currently, the clock, data, and latch are in pins 13, 12, and 11.

What a coincidence. Those ARE the pins it needs to be connected to for SPI to work, seeing as how those ARE the SPI pins.

clock on 13, data out on 11, latch on 10. 12 is data in when reading data.