I am learning arduino, and one of the things that I have been learning to program is a haptic driver. The attached program will include some lines that relate back to the haptic driver.
I am trying to send a string from a python tkinter module in order for the preprogrammed arduino to play a sequence of up to 8 different haptic effects. The string is intended to make the processing of data faster with the arduino, making it easer for the user to use and test the driver. I have been able to get the code to work in a previous version where it parses serial for integers and waits for each input, but I had to code delays into python in order to make the timing correct.
Mainly, when I send the string from python or send a sequence of numbers in the serial monitor in arduinio like such "4 20 30 40 50 4" the driver does not respond and I just end up looping back to setup (as intended) but with no result
A problem could be the fact that I don't know if arduino is recieving the string or if it is just recieving as a sequence of numbers, even though in python I made it very specific that it should write a string, not an int to serial, and in other examples I have seen strings work in similar ways.
In hterm, I am able to check what python is sending, and it seems to be the correct complete string. I will include my code and the screenshot from hterm
Note that the first \n is when the port connects on hterm, not what is recieved from python, which does not affect the program, in previous iterations as well.
This is the python code, at least the non-HMI part of the code :
def sendSignal():
#global isOpen
#isOpen=False
comPort="COM6"
serialInst=serial.Serial()
micr=serial.Serial(comPort,
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1,
)
#micr.write(1)
#time.sleep(3)
numEffects=(int(regSlider.get()))
numEffectsAsc=(str(int(regSlider.get())))
effectConfirmation=[]
effectConfirmation.append(str(int(effects.index(efCom1.get()))))
effectConfirmation.append(str(int(effects.index(efCom2.get()))))
effectConfirmation.append(str(int(effects.index(efCom3.get()))))
effectConfirmation.append(str(int(effects.index(efCom4.get()))))
effectConfirmation.append(str(int(effects.index(efCom5.get()))))
effectConfirmation.append(str(int(effects.index(efCom6.get()))))
effectConfirmation.append(str(int(effects.index(efCom7.get()))))
effectConfirmation.append(str(int(effects.index(efCom8.get()))))
numReps=str(int(repSlider.get()))
fullInput=numEffectsAsc+" "
for i in range(numEffects):
fullInput+=effectConfirmation[i]+" "
fullInput+=numReps+"\n"
micr.write(fullInput.encode("ASCII"))
micr.read_until("\n")
this is the arduino code:
#include <Wire.h>
#include "Adafruit_DRV2605.h" // You can download this library below
Adafruit_DRV2605 drv; // Set the DRV2605 i2C Address and Driver
int effects[10]={0,0,0,0,0,0,0,0,0,0};
int numRegs = 0;
int numEffect = 0;
int repeatCount = 0;
String input="";
void setup() {
Serial.begin(9600);
Serial.println("14CORE | Código de Prueba para DRV2605");
delay(1000);
if (!drv.begin()) {
//Serial.println("Error al inicializar DRV2605");
while (1);
}
drv.selectLibrary(6);
drv.setMode(DRV2605_MODE_INTTRIG);
//input=Serial.readString();
}
void loop() {
if(Serial.available()>0){
drv.writeRegister8(DRV2605_REG_MODE, 0x00);
//input=Serial.readString();
input="2 30 40 4";
int index=0;
int count=0;
while (input.indexOf(" ")!=-1){
index = input.indexOf(" ");
effects[count]=(input.substring(0,index-1)).toInt();
count++;
input.remove(0,index);
}
int e=0;
for (int i=0; i<=10; i++){
if (effects[i]!=0){
drv.writeRegister8(DRV2605_REG_WAVESEQ1 +i, effects[i]);
}
}
while(effects[e]!=0){
e++;
}
RepeatCount(effects[e]);
delay(2000);
setup();
}
}
void RepeatCount(int reps) {
for (int j = 0; j < reps; j++) {
drv.go();
delay(1000);
}
return 0;
}
I should also probably mention that any "drv" methods are being used correctly and compile correctly, as they have been implemented correctly before, but if anything could be wrong it could be the writeRegister function, but it is a uint_8 parameter for both, and the register referenced in the first parameter (the correct allocation for the first effect register) is written in hexcode as 0x03(which in a normal int is just 3, if I am not mistaken) and the for loop also has worked before.
I tried to be as descriptive and detailed as possible, but if anyone has any idea in how to fix my problem, please let me know, and thank you for reading.