Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Code: Code for the Arduino Sticky

A topic by Amanda created Sep 08, 2017 Views: 733 Replies: 7
Viewing posts 1 to 4
Host

So first: I am not a programmer, I'm just passing along what my programmer is telling me. If you have any questions, I'll check with them and see what they have to say about it.  I will try and have a empty Unity project up tomorrow with this already in place, but this code will help for anyone that is not using Unity. 


This is the code on the Arduino that's sending out the serial data. 


byte buttonstates[9];

void setup() {
  buttonstates[8] = 1;
  for(int i = 0; i< 54; i+=1){
    pinMode(i,INPUT_PULLUP);
    Serial.begin(9600);
  }
}

void loop() {
  for(int i = 0; i< 8; i+=1){
    buttonstates[i] = 0;
  }
  for(int i = 0; i< 54; i+=1){
    if(digitalRead(i) != HIGH){
      buttonstates[(i) / 7] |= 1 << ((i) % 7 + 1);
      //Serial.println(i);
    }
  }
  Serial.write(buttonstates, 9);
  delay(20);
}



Please share your code/projects if you've found a way to make it work with non-Unity engines! 

Is there a reason you didn't wire them in a normal grid (with diodes for NKRO) so you could read all 100 keys with only one arduino and 20 I/O lines?

Host

Two primary reasons: 

1. We didn't do a Matrix because we didn't know a way to do a matrix that would allow for simultaneous presses, which seemed important for this board. A matrix would probably be a more efficient system, which actually feeds into the second reason. 

2. It was super cheap and relatively straight forward to do it the way that we did. I'm using Elegoo Mega 2650's, which are $12 a piece and with a docking station attachment I also don't have to soder. I have a pile of I/O expanders I bought to do it the other way, but this seemed a touch more efficient . For a larger scale project I might go the I/O expander route, but with a prototype I didn't want to necessarily soder and then, upon finding that I'd done it wrong, de-soder and then soder again. I have steady hands, but I don't know if I have "soder 100 tiny wires to tiny boards" steady. 

Ah, OK.  To detect all keys with a matrix you just have to add a diode per key so the electricity can't flow back the "wrong way" through the matrix and confuse things. Then you loop over the rows powering up one at a time and reading off the columns. But if you don't want to solder...yeah, you'd have to add a breadboard or two, or get wire wrap tools or something.

I see you also don't have any debouncing code on the Arduino.  Are you doing that on the Unity side, or just writing games where it doesn't matter?

Host

The games we were developing didn't require it, but we'll add it to the Arduino code.

I honestly believe that Serial.begin(9600); must be outside for loop, please report it to your developer

Host

They say "Yes, but it works." 


They might work on fixing the code to streamline it. 

Host

I will be supplying you all with a configuration file for the board, pointing out which buttons are which order as far as the computer is concerned early next week! There was a slight hold up in terms of physical hardware malfunctioning, but it will be corrected ASAP