Hello Arduino Community, I am trying to control data processing between Arduino UNO(Master) to the shift register-controlled switches(Slave).
The whole schematic is simple, I send 1-byte value from PC(Master) to the Arduino(slave) through pyserial, which is read serially by the same Arduino which now becomes a Master to control the shift register(new slave). The idea is I have 8 by 8 switch matrix at the new slave, so 8 bit(1 byte) information will control the on-off state of the switch. The switch IC is daisy chained, so ultimately I will be sending 8 bytes from PC to Arduino.
Unfortunately, it is not working as I intended and I'm yet to identify the core issue. Following are my codes for
Python:
import serial
#make dsrdtr true to not lose the program compiled on the arduino
ser = serial.Serial(port='COM3', baudrate=9600, bytesize=8, parity='N',dsrdtr=True)
#value = 0 should correspond to closing all the switches. List of all zeros should close everything
Value = [0, 0, 0, 0, 0, 0, 0, 0]
#encode the value into an array of bytes
Sending = bytearray(Value)
#send
ser.write(Sending)
simply explained, I generate list of values between 0 to 255, convert it to a bytearray, and then send it to the arduino serially. I have checked that the bytearray can be used for serial.write().
And for the Arduino:
#include <SPI.h>
int SwitchArray;
const int ChipSelectPin = 10;
int i=0;
int flag;
void setup() {
// init serial port baud rate
Serial.begin(9600);
pinMode(ChipSelectPin, OUTPUT);
Serial.setTimeout(5);
//initialize SPI to set pin 10,11,13 for SS, DATA, SCK
SPI.begin();
}
void loop() {
// check serial
if ( Serial.available()>=0){
// cast the string read in an integer
SwitchArray = Serial.read();
flag= 1;
}
if (flag == 1){
//turn on the chip select
digitalWrite(CS, LOW);
//Transfer the received value,
SPI.transfer(SwitchArray);
i++;
//Do this 8 times, at every transfer,the previous value
//will be sent to the next line in the daisy chain
//Once all 8 bytes have been transferred
if(i==8){
//Put Chip select to high
digitalWrite(CS, HIGH);
i=0;
}
//reset the flag everytime a value is transferred via SPI
flag = 0;
}
}
}
And it does not work, please bear in mind the python code is extensively longer but I have shortened it for the sake of simplicity when asking in this forum.
Now, the reason I've chosen to ask in the interfacing category is that I suspect the issue to be at the PC-Arduino interface.
When I try to just send a value from Arduino by writing
Spi.transfer(0)
and loop it eight times, it successfully closes the switches(more accurately, I externally receive a signal that indicates that the switches are off). When doing this, TX LED blinks.
However, if I try to send 0 through python from PC to Arduino, then it does not seem to perform like SPI.transfer(0) case. Likewise, only blink I see is RX but not TX.
So my question is, Can you receive a value from PC to UNO through serial, convert it to an integer, and then transfer it through SPI (Within the context of what I'm trying to achieve above) If yes, what seems to be the error I'm facing but not detecting?
I have not seen a question revolving similar set up as mine so I've decided to make a new topic. Apology in advance if an identical question was asked before. Moreover, I will be happy to receive any general coding feedback (don't write this don't write that) since I'm a total newbie in any programming language.
If it matters, I'm using windows system.