Changing "keyPressed" to Arduino buttons

After getting some help here to randomize my work more, I am currently trying to add 1 small addition which is required for my school project : usage of Arduino, however, the addition can be as small or as major as I like it to be.

ArrayList<TekenLine> lineList = new ArrayList();

TekenLine drawing;

String[] buttonLog;
String [] buttonLog2;
char[] letters;
int charIndex;
int minAngle=19999, maxAngle=-1000; 
float offset=0;
int sceneCounter = 0;
boolean shooterIsGekozen = false;
boolean mmoIsGekozen = false;


void setup() {
  size(600, 800);

  if (sceneCounter == 0) {
    background(255);
    fill(0, 0, 0);
    textSize(20);
    text("Kies je genre", 100, 100);
    text("W = shooters", 100, 200);
    text("S = MMOs", 100, 300);

  }

  
  drawing = new TekenLine(int(random(20, width-30)), int(random(20, height-30)), 0);
}

void draw() {  
    
  if (sceneCounter == 1 ) {

    background(255);
  
    for (char c1 : letters) {
      int intValue = int (c1); 
      if (intValue<minAngle) {
        minAngle=intValue;
      }
      if (intValue>maxAngle) {
        maxAngle=intValue;
      }
    }

    int intValue = int (letters[charIndex]); 

    float angle = map(intValue, 
      minAngle-10, maxAngle+10, 
      0, 360); 
    println(angle);

    float r = 8; // speed  
    float x =  r * cos(radians(angle+offset));
    float y =  r * sin(radians(angle+offset));


    //shooters - rood palet
    if (shooterIsGekozen) {
      switch(letters[charIndex]) {
      case 'a': 
        drawing.kleur=#DC1414;
        break;
      case 'w': 
        drawing.kleur=#FF0000;
        break;
      case 's': 
        drawing.kleur=#B22222;
        break;
      case 'd': 
        drawing.kleur=#F64D4D;
        break;
      case 'f': 
        drawing.kleur=#FFFFFF;
      }
    }

    //MMORPGs - oranje/geel palet
    if (mmoIsGekozen) {
      switch(letters[charIndex]) {
      case 'a': 
        drawing.kleur=#FFD700;
        break;
      case 'w': 
        drawing.kleur=#FF8C00;
        break;
      case 's': 
        drawing.kleur=#FF6600;
        break;
      case 'd': 
        drawing.kleur=#FFAE50;
        break;
      case '1': 
        drawing.kleur=#FFA500;
      }
    }
    
    charIndex = (charIndex + 1) % letters.length;
    offset+=random(0.4, 1.3);

    drawing.setSnelheid(x, y); 
    drawing.moveLine();
    drawing.bounceLine();
    lineList.add(drawing.copy());

    for (TekenLine tl : lineList) {
      tl.drawLine();
    }
  }
}


void keyPressed() {
    if (key == 'w') { 
      buttonLog = loadStrings("buttonLog.txt");
      letters = buttonLog[0].toCharArray();
      shooterIsGekozen = true;
      mmoIsGekozen = false;
      sceneCounter = sceneCounter + 1;
    }


    if (key == 's') {
      buttonLog2 = loadStrings("buttonLogMmo.txt");
      letters = buttonLog2[0].toCharArray();
      mmoIsGekozen = true;
      shooterIsGekozen = false;
      sceneCounter = sceneCounter + 1;
    }
  }

the class:

class TekenLine {
  float x;
  float y;

  float lineWidth;
  float lineHeight;

  float xSnelheid;
  float ySnelheid;
  
  color kleur;
  
  TekenLine(float tempX, float tempY, color tempKleur) {

    x = tempX;
    y = tempY;
    lineWidth = 10;
    lineHeight = 10;
    kleur = tempKleur;
  }

  void drawLine() {
    noStroke();
    fill(kleur);
    rect(x, y, lineWidth, lineHeight, 20);
  }

  void moveLine() {
    x += xSnelheid;
    y += ySnelheid;
  }
  
  TekenLine copy() {
   return new TekenLine(x, y, kleur); 
  }

  void bounceLine() {

    if (x >= width-20) {
      xSnelheid = -abs(xSnelheid);
    } else if (x <= 20) {
      xSnelheid = abs(xSnelheid);
    }

    if (y >= height-20) {
      ySnelheid = -abs(ySnelheid);
    } else if (y <= 20) {
      ySnelheid = abs(ySnelheid);
    }
  }
  void setSnelheid(float tempXSnelheid, float tempYSnelheid) {
    
    boolean xIsNeg=false; 
    boolean yIsNeg=false;
  
    if (xSnelheid<0)
      xIsNeg=true;
    if (ySnelheid<0)
      yIsNeg=true;

    xSnelheid=tempXSnelheid;
    ySnelheid=tempYSnelheid;

    if (xIsNeg) 
      xSnelheid*=-1;
    if (yIsNeg) 
      ySnelheid*=-1;
  }
}

Repeating myself, but: My code takes inputs from a .txt file and draws it out on-screen. With the help of user ‘Chrisir’, it got more randomized. My only issue left was how I could switch between 2 genres.

I have made a color palet for both the ‘Shooter’-genre and ‘MMO’-genre. I managed to write a function with keyPressed to read in 1 or the other. The choice can be made on a screen right when you boot the program (sceneCounter 0), the 2nd screen goes on on the choice you made. I let it get the characters first from the correct file I need, which is either the buttonLog for the shooters (TOP) or the one for the MMO input (BOTTOM). I only used to have the top code as my only piece of code, and switched the .txt file-name between the brackets to change, but that is of course not very smooth.

//buttonlog file for shooters
buttonLog = loadStrings("buttonLog.txt");
      letters = buttonLog[0].toCharArray();

//buttonlog file for MMOs
buttonLog2 = loadStrings("buttonLogMmo.txt");
letters = buttonLog2[0].toCharArray(); 

then let it choose the correct color palet with a Boolean check.

I want to convert this piece of code so that I can also use it with 2 arduino-buttons.

void keyPressed() {
    if (key == 'w') { 
      buttonLog = loadStrings("buttonLog.txt");
      letters = buttonLog[0].toCharArray();
      shooterIsGekozen = true;
      mmoIsGekozen = false;
      sceneCounter = sceneCounter + 1;
    }


    if (key == 's') {
      buttonLog2 = loadStrings("buttonLogMmo.txt");
      letters = buttonLog2[0].toCharArray();
      mmoIsGekozen = true;
      shooterIsGekozen = false;
      sceneCounter = sceneCounter + 1;
    }
  }

I already tried changing the

void keyPressed()

to

void anotherName() {
}

and put this in the setup();, but when putting it in the setup or even in the draw, it won’t register the keypresses anymore. even if i write

if (keyPressed == true) {
 if (key == ......................)
}

it still won’t seem to pick up on it.

How could I change that piece of code around so I can use both the keyboard shortcut AND/OR button to choose?

so basically :

void something();

if (key == 'something' || left arduino button) {
 obtain letters from text files and scenecounter + 1.
}

Many thanks!

1 Like

For making an event with your Arduino buttons you can use the serialEvent() function.

https://processing.org/reference/libraries/serial/serialEvent_.html

See if that can help you.

2 Likes

Thanks for the reply, I forgot to menton the following yet :

  1. We have used the “StandardFirmata” example and uploaded it to our arduino from that one arduino program (and we wont be using it anymore)
  2. We also use the Arduino (firmata) plug-in and imported the Serial. So the top of my code now includes:
import processing.serial.*;
import cc.arduino.*;

Arduino arduino; 

Does that still require the serialEvent() as you listed, or should I change my approach on that (Still a newbie with this)?

Also, I have been fooling around with it now. I noticed that when I am println-ing my 2 buttons, they do change their value from 0 to 1 in the draw(); but when I try it in the setup(); it doesn’t seem to change value. Anything I could do to get this working?

Alright so I managed fix it myself, made a stupid mistake with the whole setup(); thing.

I took my

if (sceneCounter == 0) {
    background(255);
    fill(0, 0, 0);
    textSize(20);
    text("Kies je genre", 100, 100);
    text("W = shooters", 100, 200);
    text("S = MMOs", 100, 300);

  }

that was in the setup, put it in the draw();

then changed the

void keyPressed() {
}

function to

void buttonEvent() {
   
  if (key == 'w' || arduino.digitalRead(7) == 1) {
      buttonLog = loadStrings("buttonLog.txt");
      letters = buttonLog[0].toCharArray();
      shooterIsGekozen = true;
      mmoIsGekozen = false;
      sceneCounter = sceneCounter + 1;
  } 
  
  if (key == 's' || arduino.digitalRead(8) == 1) {
      buttonLog2 = loadStrings("buttonLogMmo.txt");
      letters = buttonLog2[0].toCharArray();
      mmoIsGekozen = true;
      shooterIsGekozen = false;
      sceneCounter = sceneCounter + 1;
  }
 }

and added it to the sceneCounter 0 in the draw();

works fine now!

3 Likes