Show Posts
Pages: [1] 2
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 03:48:03 pm
Quote
reading code which can't possibly compile

OK, so I had a few problems translating the code over to the Arduino environment, but just because you can't test the original code doesn't mean it was shoddy enough to poke holes in it.

I'll tell you what, it was written and compiled using Bloodshed Dev-C++, so it was compiled using gcc, essentially the same compiler as the Arduino environment compiles with.

You know, it took me ages to hack out that original code and the sense of accomplishment I felt when I finally got it to do what I wanted it to do was destroyed in mere minutes on here. You had an excellent opportunity to help a newcomer to the Arduino platform embrace it, but instead you all decided that you'd rather just sit there poking holes in my code.

I think it's not my coding skills that need more polishing as much as your ability to render effective assistance.

EDIT: Anyway, as I said, I'm done with this place. Lock the thread, delete my account and I'll move on somewhere else.
2  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 03:13:29 pm
Quote
I'm now quite pleased that I have an example thread where a simple program was made so complex that it defeated the author who claimed they'd got it working on different hardware (even though they didn't seem to know the syntax of the C/C++ language).

OK, you know what? I couldn't care less if you don't believe I got it working on the old platform, I know that it works because it compiles and does what it was written to do. It's not my fault you don't have the board I originally wrote it for, or that for something that is much older than the Arduino technology-wise, the SDK for it  appears to have far better and more user-friendly I/O handling capabilities.

So yeah, go ahead and mock the n00b and by all means, use me as an example of someone who apparently can't code. I'll stick with my working code, thank you very much.

I'm done here, if an admin could come along and delete my account and this thread please.
3  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 02:01:09 pm
OK, I think that rather than try to shoehorn the old code into the Arduino, I'm going to have to rewrite this from scratch. stick with the code that works and use the K8055.

Thanks for all the help, but as the old adage goes, if it ain't broke, don't fix it.
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 01:34:51 pm
Well, for the contestant buttons, I'm using some handsets scavenged from an interactive DVD quiz game. These have 5 buttons, each with a different value resistor in series with it. The original control box used the different voltage potentials to distinguish which button had been pressed on each handset.

The only button I could get the Arduino to pick up when I tested it was the one with the 1K resistor.
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 12:09:54 pm
I think I've got it, got a clean compile with this:

Code:
void CheckInputs(boolean screen = true);  //passes a true value to screen
                                            //and sets CheckInputs to perform
                                            //the initial input check.

boolean inputflag = false;                  //the initial input error check
boolean exitflag = false;
int x;
const int buttonPins[] = {2, 3, 4, 5, 6};

void setup()
{
    Serial.begin(9600);
}

void loop()
{
  
  CheckInputs();
  
  Serial.println("Initial Check Complete");
  Serial.println("Enter number of questions");
  x = Serial.read();
  
  for (int i = 0; i < x; i++)
  {
    do
    {
      CheckInputs(false);

      if (digitalRead(buttonPins[4]));
      {
        for (int a= 0; a <=4; a++)
          {
            digitalWrite(buttonPins[a], LOW);
          }
        inputflag = false;
        exitflag = true;
      } while (exitflag == false);
    }
    while (digitalRead(buttonPins[4]));

    exitflag = false;
    
    
    
  }
}

void CheckInputs(boolean screen)
{
  int i;
  for (i = 0; i <=4; i++)
  {
    boolean inputstatus = digitalRead(buttonPins[i]);
    
    if (inputstatus)
    {
      Serial.print("Input ");
        Serial.print(i+1);
        Serial.println(" depressed");
      inputflag = true;
    }
    else
    {
      if (i != 4)
        if (!inputflag)
        {
          Serial.print("Contestant ");
            Serial.print(i+1);
            Serial.println(" buzzed!");
          inputflag = true;
        }
    }
  }
  
  if (!inputflag && screen)
  {
    Serial.println("No inputs depressed");
  }
  
}

Hopefully, this will work.

EDIT: Nope, it just keeps looping through the initial check saying all inputs are pressed. smiley-sad
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 10:37:26 am
Code:
void CheckInputs(boolean screen = true);  //passes a true value to screen
                                          //and sets CheckInputs to perform
                                          //the initial input check.

boolean inputflag = false;                //the initial input error check
boolean exitflag = false;
int x;
const int buttonPins[] = {2, 3, 4, 5, 6};

void setup()
{
    Serial.begin(9600);
}

void loop()
{
  
  CheckInputs();
  
  Serial.println("Initial Check Complete");
  Serial.println("Enter number of questions");
  x = Serial.read();
  
  for (int i = 0; i < x; i++)
  {
    do
    {
      CheckInputs(false);
      
      if (digitalRead(buttonPins[6]));
      {
        for (int a= 2; a <=6; a++)
          {
            digitalWrite(buttonPins[a], LOW);
          }
        inputflag = false;
        exitflag = true;
      } while (exitflag == false);
      
    while (digitalRead(buttonPins[6]));
      
    exitflag = false;
    }
    
    
  }
)

void CheckInputs(boolean screen)
{
  int i;
  for (i = 2; i <=6; i++)
  {
    boolean inputstatus = digitalRead(buttonPins[i]);
    
    if (inputstatus)
    {
      Serial.println("Input " i " depressed");
      inputflag = true;
    }
    else
    {
      if (i != 6)
        if (!inputflag)
        {
          Serial.println("Contestant " i-1 " buzzed!");
          inputflag = true;
        }
    }
  }
  
  if (!inputflag && screen)
  {
    Serial.println("No inputs depressed");
  }
  
}

OK, so this is how far I've got so far.
7  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 09:41:04 am
Quote
The "else;"s do nothing - the fact that there's a semicolon after them signals that.
They are redundant, as is the "return;".

So they are, just test compiled the original code without them and it returned no errors.

Quote
But you overwrite it at the beginning of every loop!

D'oh!

Just goes to show how useful it is having an objective pair of eyes go over your code, lol.
8  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 09:20:11 am
Quote
Not every if statement needs an else. If the else is not doing anything, get rid of it.

These do. If you read through the function's logic you'll see that they are all dependent on one another. There was yet another if...else in there, but I managed to refactor them into one. Unfortunately, that was the only one I have been able to refactor without breaking the logic. If you can find a way to reorganise it and have it perform the same function, I would appreciate the help.

Quote
The else; look like errors. At least, errors waiting to happen.

The original code compiles with no errors, so I transposed it almost verbatim, editing for the differences in syntax between C and Arduino. If the Arduino compiler throws a hissy fit, it's due to its interpretation of the C language, not my initial code.
Quote
It's an array, not a function.

Good catch, thanks.

Quote
Not much point initiailising the flag, if you're going to overwrite it:

Unless of course, you need it resetting at the beginning of each loop.
9  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 06:26:45 am
So, CheckInputs would be:

Code:
void CheckInputs (boolean screen)
{
  int i;
  for (i = 2; i <=6; i++)
  {
    boolean inputstatus = false;
    
    inputstatus = digitalRead(buttonPins[i]);
    
    if (inputstatus)
    {
      Serial.println("Input " i " depressed");
      inputflag = true;
    }
    else
    {
      if (i != 6)
        if (!inputflag)
        {
          Serial.println("Contestant " i-1 " buzzed!");
          inputflag = true;
        }
        else;
      else;
    }
  }
  else;
  
  if (!inputflag && screen)
  {
    Serial.println("No inputs depressed");
  }
  else;
  
  return;
}
10  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 05:42:35 am
Ah, so if I use:

Code:
int buttonPins[] = {2, 3, 4, 5, 6};

I can use that to step iteratively through the input scanning routine?
11  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 05:29:40 am
Except that I need to declare 5 input pins, so I would need to declare:

Code:
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;

Unless I can assign a variable to a single buttonPin declaration in order to step through the pins iteratively.

Essentially, what the K8055 ReadDigitalChannel() function does is scan the input bus for changes to a specific channel number, but you can scan multiple channels at once by feeding it a variable instead of a channel number and stepping through iteratively.
12  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 04:41:25 am
At the moment, playing a buzzer sound is not important.

One thing I did notice is that because you have to set up a constant for each input, something like this:

Code:
digitalRead (buttonPin(i));

won't work, unless I'm missing something blindingly obvious.
13  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 03:40:36 am
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.
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help needed to convert some code. on: April 16, 2010, 03:21:54 am
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.
15  Forum 2005-2010 (read only) / Syntax & Programs / Help needed to convert some code. on: April 16, 2010, 02:52:24 am
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:
#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;
}
Pages: [1] 2