bsch2734 / SpiShiftRegisterChain

A simple Arduino library for controlling any length of chained 595 style shift registers over the built in SPI bus
The Unlicense
0 stars 1 forks source link

Simple Example #1

Open Nezaemmy opened 1 month ago

Nezaemmy commented 1 month ago

Hello sir, I would like to ask you if you have any simple example of how to use this library. like switch ON and OFF of any pin of shift register.

bsch2734 commented 1 month ago

Thanks for reaching out. I have been meaning to get the documentation for this up for a while so this will be a good excuse to finish that. I will try to post something tonight.

On Monday, October 7, 2024 at 10:33:46 AM CDT, Nezaemmy ***@***.***> wrote:  

Hello sir, I would like to ask you if you have any simple example of how to use this library. like switch ON and OFF of any pin of shift register.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Nezaemmy commented 1 month ago

Thank you

bsch2734 commented 1 month ago

Documentation was added in bfc9f387afa272f2fdbd6b780677add071996595. Please feel free to open another issue if you have any further questions.

Nezaemmy commented 1 month ago

Thank you for the good explanation, I have one question. in the note is written For each additional shift register in the chain:

Connect the data chaining pin, labeled Q7’ or Q7’ Out on most 8 bit registers, of the current register to the Data pin of the next register in the chain. Ground: Connect the GND pins on the shift register and Arduino.

What happen to the Latch Pin and SPI Clock Pin of the additional shift register, we leave those pins open or we connect them in parrallel with the same pins of first shift register? like in the image below 74hc595

bsch2734 commented 1 month ago

Your diagram is correct. You need to connect the latch and clock pins of all registers in the chain. I will clarify the documentation later.

Nezaemmy commented 1 month ago

hello Sir I get this error msg c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp: In member function 'void SpiShiftRegisterChain::allocateMemory()': c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:78:34: error: no matching function for call to 'max(int, unsigned int&)' 78 | _dataArray = new byte[max(4, _dataLengthBytes)]; | ~^~~~~~~ In file included from C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/algorithm:61, from C:\Users\Emugwaneza\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.0-RC1\cores\esp32/Arduino.h:188, from c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.h:5, from c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:1: C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algo.h:5805:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)' 5805 | max(initializer_list<_Tp> l, _Compare comp) | ^~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algo.h:5805:5: note: template argument deduction/substitution failed: c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:78:34: note: mismatched types 'std::initializer_list<_Tp>' and 'int' 78 | _dataArray = new byte[max(4, _dataLengthBytes)]; | ~^~~~~~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algo.h:5795:5: note: candidate: 'template constexpr _Tp std::max(initializer_list<_Tp>)' 5795 | max(initializer_list<_Tp> l) | ^~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algo.h:5795:5: note: template argument deduction/substitution failed: c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:78:34: note: mismatched types 'std::initializer_list<_Tp>' and 'int' 78 | _dataArray = new byte[max(4, _dataLengthBytes)]; | ~^~~~~~~ In file included from C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/specfun.h:43, from C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/cmath:3699, from C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/math.h:36, from C:\Users\Emugwaneza\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.0-RC1\cores\esp32/esp32-hal.h:30, from C:\Users\Emugwaneza\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.0-RC1\cores\esp32/Arduino.h:36: C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)' 303 | max(const _Tp& a, const _Tp& b, _Compare comp) | ^~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algobase.h:303:5: note: template argument deduction/substitution failed: c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:78:34: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'unsigned int') 78 | _dataArray = new byte[max(4, _dataLengthBytes)]; | ~^~~~~~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algobase.h:257:5: note: candidate: 'template constexpr const _Tp& std::max(const _Tp&, const _Tp&)' 257 | max(const _Tp& a, const _Tp& b) | ^~~ C:/Users/Emugwaneza/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2405/xtensa-esp-elf/include/c++/13.2.0/bits/stl_algobase.h:257:5: note: template argument deduction/substitution failed: c:\Users\Emugwaneza\Documents\Arduino\libraries\SPIShiftRegister\src\SpiShiftRegisterChain.cpp:78:34: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'unsigned int') 78 | _dataArray = new byte[max(4, _dataLengthBytes)]; | ~^~~~~~~

exit status 1

Compilation error: exit status 1

bsch2734 commented 1 month ago

I think I see where this is coming from, but I cannot reproduce it. Can you provide me with a small bit of code that should show the error? My test code looks like this and compiles fine. Let me know if it does not for you:

#include <SpiShiftRegisterChain.h>

SpiShiftRegisterChain chain(3,2);

void setup() {}

void loop() {
  chain.toggleBit(3);
  delay(1000);
}

Once I can reproduce the error on my end it should be an easy fix.

Nezaemmy commented 1 month ago

I used this example

include

include

const int latchPin = 10; const int dataLengthBytes = 4;

SpiShiftRegisterChain shiftRegister(latchPin, dataLengthBytes);

void setup() { SPI.begin(); shiftRegister.setBitOn(0); // Turn on the 1st bit and write data shiftRegister.setBitOff(1, false); // Turn off the 2nd bit without writing shiftRegister.writeData(); // Apply all changes at once delay(1000);

shiftRegister.toggleBit(0); // Toggle the 1st bit and write data delay(1000); }

void loop() { // Your main code here }

bsch2734 commented 1 month ago

Interesting, that also compiles fine on my machine. I think you may just have stricter compiler settings about automatic type conversion than I do. I can work on a fix but I'll need you to check it since I cannot reproduce. It might not be up before Monday though.

Nezaemmy commented 1 month ago

I use ESP32S3 board not arduino AVR board that is the reason it gives me the error. I think it is only compatible with arduino avr board not ESP32 board

bsch2734 commented 1 month ago

I was able to reproduce the issue by compiling for ESP32. I have not used that board but based on my research the code should still work. I created a new release 671de19f104f097b233b8d49783f8d3f22e6ab6d that should fix the compiler error you were getting. Let me know if this works.

Nezaemmy commented 1 month ago

The compiler error is fixed but the the example is not working how it supposed to work on esp32. if you have esp32 board try to run this code

include

include

const int latchPin = 10; const int dataLengthBytes = 4;

SpiShiftRegisterChain shiftRegister(latchPin, dataLengthBytes);

void setup() { SPI.begin(); shiftRegister.setBitOn(0); // Turn on the 1st bit and write data shiftRegister.setBitOff(1, false); // Turn off the 2nd bit without writing shiftRegister.writeData(); // Apply all changes at once delay(1000);

shiftRegister.toggleBit(0); // Toggle the 1st bit and write data delay(1000); }

void loop() { // Your main code here }

bsch2734 commented 1 month ago

I don't have an ESP32 to test on but I was able to look up some info and it should be compatible as is. Did you try running on an Arduino to make sure your shift register setup is correct? On ESP32 will need to use the VSPI pins. I'll get an ESP32 and see how it goes.

Nezaemmy commented 1 month ago

yes shift register setup is correct, for me I use esp32s3 board, I don't know if it use VSPI pins as ESP32

bsch2734 commented 4 weeks ago

I have acquired an ESP32 S3 dev board and confirmed that the library works with it. I ran the example above and it works. However all that example does is set bit 0 (i.e. the first bit of the first chained register) to high and the rest to low. It may be useful to try a continuously changing output to see it in action. You can set the loop function to something like this:

void loop() {
    shiftRegister.toggleBit(0);
    shiftRegister.toggleBit(3);
    delay(1000);
}

This will change the state of bits 0 and 3, as counted from the first output of the first chained register, every second. Here is my wiring diagram. Make sure you use the same pins, or whichever pins are VSPI on your module. image

Nezaemmy commented 3 weeks ago

I'm using the same diagram and I use latchPin =10 in the code. for example I want to switch on bit 3

#include <SPI.h>
#include <SpiShiftRegisterChain.h>

const int latchPin = 10;
const int dataLengthBytes = 3;

SpiShiftRegisterChain shiftRegister(latchPin, dataLengthBytes);

void setup() {
SPI.begin();

}

void loop() {

   // shiftRegister.setBitOn(3, true);
    shiftRegister.toggleBit(3);
}

I don' t know why nothing happen on the bit 3

When I try to use

#include <SPI.h>
#include <SpiShiftRegisterChain.h>

const int latchPin = 10;
const int dataLengthBytes = 3;

SpiShiftRegisterChain shiftRegister(latchPin, dataLengthBytes);

void setup() {
SPI.begin();

}

void loop() {
  shiftRegister.toggleBit(0);
    shiftRegister.toggleBit(3);
    delay(1000);
}

I think this should blink bit 0 and bit 3, but it is mixing blinking from bit 0 to bit 23 and OFF.

The set up is ok, it is working with other shift register library