Go Down

Topic: Help needed to convert some code. (Read 3 times) previous topic - next topic

flapjackboy

OK, a bit of background here. A few years ago, I bought a Velleman K8055 USB interface board and whilst tinkering with it, I developed a rather nice quiz buzzer program for it.

Fast forward to today and I start hearing about Arduino, so I order a Duemilanove to tinker with, hoping to convert my quiz buzzer program over to it, thinking that it shouldn't be too hard...

Well, it turned out to be more difficult than I thought and I could use a little help from the community with it.

Here's the original code:

Code: [Select]
#include <stdio.h>
#include <iostream>
#include "K8055D_C.h"
#include "resource.h"
#include "conio.h"
#include <cstdlib>
#include "windows.h"

using namespace std;

void CheckInputs(bool screen = true);   // passes a true value to screen
                                       // and sets CheckInputs to perform                                        
bool inputflag = false;                 // the initial input error check
bool exitflag = false;
int x;

int main()
{
     // open communication with K8055D
     OpenDevice(0);

     // check for sticky inputs
     CheckInputs();

     cout << "Initial Check Complete" << endl;
     cout << "Enter number of questions: ";
     cin >> x;
     cout << "Press any key to continue...";
     _getch();
     system("CLS");      
     for (int i = 0; i < x; i++)
     {
       // cout << i; // used for testing purposes
       
         do
         {
               CheckInputs(false);
   
               if (ReadDigitalChannel(5))
               {
                     ClearAllDigital();
                     system("CLS");
                     inputflag = false;
                     exitflag = true; // get out of do...while loop
               }
         } while (exitflag == false); // loop while button 5 is not depressed
         
         while (ReadDigitalChannel(5));
         
         exitflag = false;
   }

     ClearAllDigital();
     cout << "Press any key to exit...";
     _getch();
     
     CloseDevice();
     return 0;
}

void CheckInputs(bool screen)          
{                                      
     for (int i = 1; i <= 5; i++)        
     {                                  
           bool inputstatus = false;      

           inputstatus = ReadDigitalChannel(i);

           if (inputstatus)
           {
                 if (screen) // If at program start, check for stuck inputs
                 {
                       cout << "Input " << i << " depressed" << endl;      
                       inputflag = true;   // Lock inputs 1-4
                 }
                 else
                 {
                       if (i != 5)         // If input 5 not pressed and
                             if (!inputflag) // if no inputs locked
                             {
                                   SetDigitalChannel(i); // Set corresponding output
                                   cout << "Contestant " << i << " buzzed!" << endl;
                                   PlaySound("buzzerheavy.wav", NULL, SND_ASYNC);
                                   inputflag = true;   // Lock inputs 1-4
                             }
                             else;
                       else;
                 }
                 
           }
           else;
     }

     if (!inputflag && screen)  // If program starts
                                              // with no stuck inputs
     {
           cout << "No inputs depressed\n" << endl;
           ClearAllDigital();
     }
     else;

     return;
}

Groove

That's the PC end of your program - you need to read about how to write sketches for the Arduino.

There are tutorials and examples over at the main Arduino site.
Per Arduino ad Astra

flapjackboy

Yes, I am aware of this. I know that loop() and setup() take the place of main() and get compiled to main() in the final uploaded code.

My main sticking point seems to be that there's no function equivalent to the K8055's ClearAllDigital() in Arduino, but also general problems with syntax matching.

Essentially, CheckInputs() is a Swiss Army Knife routine that performs different functions depending on where in the program it is called and what data is passed to it.

During the initial program startup, it checks that all the inputs are clear, then shifts to detecting buzzer presses and locking out other players. Finally, it checks to see if the reset button has been pressed by the quizmaster.

Groove

I'm confused - are you aiming to run the Arduino as simply an I/O board, and handle the interface on the PC, or do you want to do it all on the Arduino, via the serial interface?
Per Arduino ad Astra

flapjackboy

#4
Apr 16, 2010, 10:40 am Last Edit: Apr 16, 2010, 10:43 am by flapjackboy Reason: 1
I want to run it all on the Arduino via the serial interface. Eventually, I want to make it self contained, using an LCD display to control everything, but for now I just need to get as close as possible to the original program as I can.

Go Up