creating a button
in
Core Library Questions
•
1 year ago
hey guys, I'm using an ID-12 RFID reader to read out tags. once two have been identified the program has to show a "challenge"they can do. at that point two buttons should appear. A fulfilled and a pass button. after trying to understand the example on processing about buttons and other Google searches is still do not understand the easiest way to make a button a know which one of the two was pressed or where i should but that into my code.... i want the buttons to appear in my void showChallenge.... any tips/ advise/ help?
// import the serial library:
import processing.serial.*;
Serial myPort; // the serial port you're using
String tagID = ""; // the string for the tag ID
String previousTagID = "" ;
int challengeNum = 0;
int maxChallenges = 12;
String[] challengeArray = {
"Each person cooking can choose an ingredient to cook with", // Challenge 1
"The eldest child can choose a parent to cook with", // Challenge 2
"The children can go to the supermakret and cook dinner with the help of a parent", // Challenge 3
"Have fun, eat somewhere els then at the dinner table", // Challenge 4
"The youngest member of the family has to choose a parent to cook dinner with", // Challenge 5
"Have a family meal fallowed by a night which can include a movie night", // Challenge 6
"The youngest child can choose a cake to bake for desert", // Challenge 7
"Cook a three course meal wich consists of an entré, a main course and a dessert", // Challenge 8
"Cook something you have not eaten before", // Challenge 9
"Use an ingredient you have not used before", // Challenge 10
"Create your own recipe wih at least two children and give it a name", // Challenge 11
"Cook with at least three family members"// Challenge 12
};
int[] challengeScores = {
8, // Challenge 1
8, // Challenge 2
11, // Challenge 3
16, // Challenge 4
8, // Challenge 5
25, // Challenge 6
7, // Challenge 7
11, // Challenge 8
6, // Challenge 9
3, // Challenge 10
12, // Challenge 11
13, // Challenge 12
};
String[] rewardArray = {
"You can choose a parent to give you breakfast in bed", // Reward 1
"You can choose where to go for dinner", // Reward 2
"You can choose wat is for breakfast this Saturday or Sunday", // Reward 3
"You can choose what to eat for dinner", // Reward 4
"You do not have to do the dishes", // Reward 5
"You can choose what to have for dessert", // Reward 6
};
int[] rewardScores = {
70, // Reward 1
80, // Reward 2
25, // Reward 3
40, // Reward 4
19, // Reward 5
25, // Reward 6
};
String person1 = "" ;
String person2 = "" ;
int scoreNatasha = 0;
int scoreJames = 0;
RectButton rectFulfilled, rectPass;
void setup() {
size(1800, 950);
// list all the serial ports:
println(Serial.list());
// change the number below to match your port:
String portnum = Serial.list()[1];
// initialize the serial port:
myPort = new Serial(this, portnum, 9600);
// incoming string will end with 0x03:
myPort.bufferUntil(0x03);
background(255, 255, 255);
textAlign(CENTER);
textSize(30);
}
void draw() {
// background(255, 255, 255);
}
void serialEvent(Serial myPort) {
// get the serial input buffer in a string:
String inputString = myPort.readString();
// filter out the tag ID from the string:
// first character of the input:
char firstChar = inputString.charAt(0);
// last character of the input:
char lastChar = inputString.charAt(inputString.length() -1);
// if the first char is STX (0x02) and the last char
// is ETX (0x03), then put the next 10 bytes
// into the tagID string:
if ((firstChar == 0x02) && (lastChar == 0x03)) {
tagID = inputString.substring(1, 11);
setPerson();
}
}
void setPerson() {
if (person1.length() < 1) {
person1 = tagID;
// Laat naam persoon 1 zien
showName(person1);
}
else if (person2.length() < 1 && tagID.equals(person1) == false) {
person2 = tagID;
// Laat naam persoon 2 zien
showName(person2);
// there are two persons, show them the challenge
showChallenge();
}
}
void showName(String tag) {
noStroke();
background(255, 255, 255);
// orange rectangle bottom
fill(255, 138, 0);
rect(750, 490, 300, 60);
//red rectqangle top
fill(248, 36, 36);
rect(750, 330, 300, 80);
//yellow rectangle colour
fill(248, 236, 36);
//yellow rectangle location
rect(670, 390, 80, 100);
//yellow rectangle location
rect(1050, 390, 80, 100);
//blue rectangle by the name
fill(21, 126, 245);
rect(750, 390, 300, 100);
//colour of text hey Natasha
fill(255, 255, 255);
// text to print and X Y location
// zoek de goeie persoon bij de tag
if (tag.equals("42004482C5")) {
text ("Hello James", 900, 450);
}
else if (tag.equals("4200446F94")) {
text ("Hello Natasha", 900, 450);
}
println("1: " + person1);
println("2: " + person2);
}
void showChallenge() {
delay(2000);
background( 255, 255, 255);
fill(0);
text(challengeArray[challengeNum], 900, 450);
challengeNum++;
if (challengeNum > maxChallenges) {
challengeNum = 0;
}
// Show buttons fullfilled / pass
// press pass: showChallenge();
// press fulfilled: showScoreBoard();
delay(5000);
}
// keep track of score board
void showScoreBoard() {
if (person1.equals("4200446F94")) {
scoreNatasha += challengeScores[challengeNum];
text ("Natasha= scoreNatasha", 900, 450);
}
if (person2.equals("4200446F94")) {
scoreNatasha += challengeScores[challengeNum];
text ("Natasha = scoreNatasha", 900, 450);
}
if (person1.equals("42004482C5")) {
scoreJames += challengeScores[challengeNum];
text ("James = scoreNatasha", 900, 450);
}
if (person2.equals("42004482C5")) {
scoreJames += challengeScores[challengeNum];
text ("Natasha= scoreNatasha", 900, 450);
}
delay(5000);
// Show list of rewards
showListOfRewards();
}
void showListOfRewards() {
resetPeople();// put at the end
}
void resetPeople() {
println("RESET");
person1 = "";
person2 = "";
}
1