Author Topic: QB64 + Arduino, connection problems  (Read 5381 times)

0 Members and 1 Guest are viewing this topic.

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
QB64 + Arduino, connection problems
« on: August 16, 2021, 08:05:20 pm »
Hello to the community. I'm working on a proyect with my dad about slot cars. We figured out that we can connect two reed switches into the "road" for reading the moment in wich the cars passes the goal line. This reed switches are connected to an Arduino UNO, that should send that information to a QB64 program that takes the time of the reading and shows it into the screen, numbre of laps, etc. There are two game modes: Practice and Race, so the Arduino must manage also traffic light, which should work in the Race mode, but in Practice mode.

I don't feel like having any problems with the program itself, but what is giving me troubles is the connection subject.

I've read the wikipedi about the OPEN COM command, quite new for me and very interesting... but a bit complex.

Of course there's an Arduino program, but these one should be minimalistic, giving most of the control to the QB64. The only purpose of the Arduino should be translating the reed swtiches to send them thru Serial and activating the traffic light according to what receives from the QB64.

I have never worked in the connection between uC and QB64 so i would like to ask some help about this topic, or if any kind soul knows a simpler solution to my problem i'm all ears.

For the moment i attempted some experiments, but every time i have a QB64 program running that open the COM port 3 (Arduino connected to the same port) it gives me the error "Unavailable device". On the other hand if i open the QB64 first and then the Arduino serial monitor ihave an error from the Arduino side in the same sense.

How is that this kind of connection of the COM ports work? Only one thing can talk to a port at a given time? I don't think that this is the case, otherwise ports won't be of much use.

That would be my questions. Thanks and cheers to all.
« Last Edit: August 17, 2021, 08:27:13 pm by odin »

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: !B64 + Arduino, connection problems
« Reply #1 on: August 17, 2021, 02:20:55 pm »
Why not get a display for the Arduino and avoid having to transfer the data to QB64 (I presume this is on a PC).
Then just display the stuff you need on the display.
I have no experience with arduino, as I work with microchip's PIC microcontrollers and interfacing with a PC (though possible -I've done it) is not easy or elegant.
Also I would have probably opted for an optical sensor rather than a reed switch.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: !B64 + Arduino, connection problems
« Reply #2 on: August 17, 2021, 03:33:01 pm »
Unfamiliar, but these this thread might be of help / interest...

http://petesqbsite.com/phpBB3/viewtopic.php?f=1&t=8276

Pete (but not the pete who owns the site linked above. He's that "other" Pete")
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: !B64 + Arduino, connection problems
« Reply #3 on: August 17, 2021, 03:56:01 pm »
Yes it used to be easy when computers had parallel (printer) ports. You had about 12 outputs and a few inputs using INP and OUT. But then came USB and displaced all the useful ports.
Arduino can do USB (does it pretend to be a COM port?), but that said the usb protocol is intricate and proprietary.   

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: !B64 + Arduino, connection problems
« Reply #4 on: August 17, 2021, 06:48:50 pm »
Hello david_uwi and Pete, thanks for your replies.

Pete: as david_uwi mentioned the idea is to connect an Arduino board to a computer via USB cable. So the thread you show here is interesting, but unrelated. I would love to had a parallel port, but as you know computer manufacturers have shifted mostly to USB ports, so they are kind of universal.

david_uwi: we are not using a display by two reasons: 1) we want a bigger screen XD and 2) more functionality, as .txt files with the times and laps proccesed by QB64, so we can have a long register of the races and practices, names of the cars to compare between them... and a long possible etc. This is why the uC program should be minimalistic, avoiding a more complex lenguage with less OUTPUT and INPUT in exchange for simple interpreter that comunicates with a more complex program, that can be expanded to God knows how much and with better periphericals for input and output. About the optical sensor vs reed switches: agree that is a better idea, but we short on hardware, We have reed switches, no problem on that. When we get the hardware we will change to optical.

So yes, we gonna connect the Arduino board to the computer by USB cable, we gonna program the uC to gives us the reading of the reed switches and control 3 transistors (one for each traffic light: conventional g,y&r). The QB64 should direct wich of the light is on according to a byte sended to the uC board and register every moment of the reed switches from what it receives from the uC

I tried this program on the Arduino:

//pines del Arduino al que están conectados los transistor que disparan las luces del semáforo
const int semaforoRojo = 10;
const int semaforoAmrillo = 9;
const int semaforoVerde = 8;
//pines del Arduino a los que están conectados los reed switch
const int reedVerde = 2;
const int reedRojo = 3;
//VARIABLES DE PROGRAMA
const int tiempoDebounce = 2000;//modificar para alterar el tiempo de espera del debounce de los reed switch, no debe ser > 32767
int valReedVnuevo;//lectura del reed verde
int valReedRnuevo;//lectura del reed rojo
int valReedVviejo;//lectura anterior del reed verde (debounce)
int valReedRviejo;//lectura anterior del reed rojo (debounce)
int readFromSerial;//lectura de lo que viene de la PC
int writeToSerial;//escritura que vamos a realizar hacie el PC

void setup() {
  Serial.begin(9600);
  pinMode (semaforoRojo, OUTPUT);
  pinMode (semaforoAmrillo, OUTPUT);
  pinMode (semaforoVerde, OUTPUT);
  pinMode (reedVerde, INPUT);
  pinMode (reedRojo, INPUT);
}

void loop() {
  valReedVnuevo = digitalRead (reedVerde);
  valReedRnuevo = digitalRead (reedRojo);
  //  Serial, println (valReedVnuevo, valReedRnuevo);
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    readFromSerial = Serial.read();
    // say what you got:
    Serial.println(readFromSerial, DEC);
  }
  delay(10);
}

with this program of QB64:

Code: QB64: [Select]
  1. DIM bytestr AS STRING * 1 'one byte transfers
  2. INPUT "COM port number #", port$ 'any COM port number available
  3.  
  4. OPEN "COM" + port$ + ":9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
  5. DO 'main loop
  6.     'receive data in buffer when LOC > 0
  7.     IF LOC(1) THEN
  8.         GET #1, , bytestr
  9.         PRINT "[" + bytestr + "]";
  10.     END IF
  11.     'transmit (send)
  12.     k$ = INKEY$
  13.     IF LEN(k$) = 1 THEN
  14.         k = ASC(k$)
  15.         IF k >= 32 THEN 'ignore control key codes
  16.             PRINT ">" + k$ + "<";
  17.             bytestr = k$: PUT #1, , bytestr
  18.         END IF
  19.     END IF
  20. LOOP UNTIL k$ = CHR$(27)
  21. CLOSE #1: PRINT "Finished!"
  22.  

The board is connected to port 3, so  imdirect QB64 to 3 as well, but i get this error. The only parameter asked for the uC seems to be the baud rate (used 9600), whic is also one of the parameters in the OPEN COM instruction, but i don't really understand if the rest of the parameters in the OPEN COM are ok. I've read the wiki about the command, but i'm not sure what kind of parameters i should be using, or if this have nothing to do with the error i'm getting.

Again, thanks for the replies and cheers.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: QB64 + Arduino, connection problems
« Reply #5 on: August 18, 2021, 02:53:45 am »
Here's one I wrote. As it says in a comment the transfer seems to be determined by the sender (independent of the baud rate in the OPEN COM statement).
Code: QB64: [Select]
  1. 'works faster than strings: FT201X I2C to USB transfer
  2. '******Connect the USB cable first!!!!!!!!!!!!!!
  3. '******then press F5****************************
  4. WIDTH 120, 70
  5. ON ERROR GOTO handler
  6. k = 1
  7. addr = 0
  8. OPEN "c:\cw1\rubbish1.txt" FOR OUTPUT AS #2
  9. OPEN "com3:115200,N,8,1,CS0,DS0" FOR RANDOM AS #1 LEN = 2 '115200 19200 9600
  10. 'the baud rate does not matter the FT201 is determining the speed
  11.     WHILE LOC(1) < 2: WEND
  12.     addr = addr + 1
  13.     GET #1, , x1
  14.     PRINT #2, USING "#######"; x1 + 60000;
  15.     PRINT USING "######"; x1;
  16.     IF addr MOD 8 = 0 THEN PRINT #2, " "
  17.     IF addr MOD 8 = 0 THEN PRINT addr
  18.     'IF x1 = 65535 THEN EXIT DO 'end of data if chip deleted
  19.     IF addr > 524287 THEN EXIT DO 'for 1MB
  20. handler:
  21. SLEEP (1)

This is reading data from a flash memory chip that is in a datalogger

Code: QB64: [Select]
  1. 'FT201X I2C to USB transfer
  2. '******Connect the USB cable first!!!!!!!!!!!!!!
  3. '******then press F5****************************
  4. DIM q AS STRING * 1
  5. q = CHR$(234)
  6. 'this will send one byte=234
  7. 'for some reason this only works with strings!
  8. WIDTH 120, 70
  9. ON ERROR GOTO handler
  10. k = 1
  11. addr = 0
  12. OPEN "com5:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1 LEN = 1 '115200 19200 9600
  13. addr = addr + 1
  14. PUT #1, , q
  15. handler:
  16. SLEEP (1)

this is sending data from the computer to the microprocessor

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: QB64 + Arduino, connection problems
« Reply #6 on: August 18, 2021, 09:54:41 pm »
Problem solved: what can be open at the same time is the serial monitor of the Arduino IDE. QB64 is perfectly communicating with the uC and i keep working on the program. Thanks both of you guys.

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: QB64 + Arduino, connection problems
« Reply #7 on: August 31, 2021, 11:58:41 am »
Hello again. The proyect keeps in route, and looking good, but i found myself in a problem i'll try to describe:

I have a dinamic array of strings that in every index has a time in the format "mm:ss.mmm". This array contains LAP TIMES, an i'm can't find a way to find the smallest time. i've tried a few combinations (almost like a trial and mistake process) and i'm very lost on how to do this.

I can imagine that there should be one of the indexes that should consider as the comparative point, that should be parsed to see the differences between minutes, seconds and miliseconds. That should be compared with another index, that should also be parsed. According to the comparation the array should be rearrenged, untill i have it from minor to mayor or visceversa. As i said i'm not sure how to do it. Or is there a better method i haven't figured out yet? Thanks to everybody.  Cheers.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: QB64 + Arduino, connection problems
« Reply #8 on: September 02, 2021, 02:46:14 am »
A simple bubble sort will suffice.  Not likely your data will be in DATA statements,
but this conveys a method of sorting.
Code: QB64: [Select]
  1. ' rank mm:ss.mmm
  2.  
  3. DIM t$(100), t!(100) '                                     text timings, timings in seconds
  4.     READ t$ '                                              read from DATA statements
  5.     IF t$ = "EOD" THEN EXIT DO '                           end of data
  6.     n = n + 1 '                                            keep track of how many timings there are
  7.     t$(n) = t$ '                                           save original timing to array
  8.     t!(n) = VAL(LEFT$(t$, 2)) * 60 + VAL(MID$(t$, 4, 6)) ' convert to seconds for sorting
  9.  
  10. sort: '                                                    bubble sort
  11. sorted = 1 '                                               assume is sorted
  12. FOR i = 1 TO n - 1
  13.     IF t!(i) > t!(i + 1) THEN '                            wrong order
  14.         SWAP t$(i), t$(i + 1) '                            swap the text elements
  15.         SWAP t!(i), t!(i + 1) '                            swap the corresponding seconds elements
  16.         sorted = 0 '                                       set flag off
  17.     END IF
  18. IF sorted = 0 THEN GOTO sort '                             loop back until in order
  19.  
  20. FOR i = 1 TO n '                                           show sorted
  21.     PRINT USING "### "; i;
  22.     PRINT t$(i)
  23.  
  24. DATA "32:12.343"
  25. DATA "28:21.544"
  26. DATA "31:34.232"
  27. DATA "34:56.544"
  28. DATA "29:02.123"
  29. DATA "27:56.321"
  30. DATA "30:22.877"
  31. DATA "30:45.434"
  32. DATA "28:12.345"
  33. DATA "27:33.555"
  34. DATA EOD
  35.  
« Last Edit: September 02, 2021, 02:49:23 am by Richard Frost »
It works better if you plug it in.