With this weekly assignment, I attempt to create a Digital version of the analogue crossbow that I designed for my midterm, using the same 2 potentiometer, 1 button remote controller.
I spent quite a while fiddling with the code in the original handshake example, trying to get it to input data from multiple sensors connected to my Arduino. That being said, when I found the sketch that gave me the syntax to do it properly, I had most of the inputs properly formatted.
I ended up having to Add a third input to the code (for my button). Basically it would register pressed as a 1 and unpressed as a 0, then send it into the processing sketch, where I could make an if statement in order to draw a mark.
void setup() { Serial.begin(9600); Serial.println("0,0,0"); pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(trigPin, INPUT); } void loop() { while (Serial.available()) { right = Serial.parseInt(); left = Serial.parseInt(); int trigRead = digitalRead(trigPin); if (trigRead == HIGH) { fire = 1; } else { fire = 0; } // Serial.print(trigRead); if (Serial.read() == '\n') { digitalWrite(3, right); digitalWrite(5, left); int sensor = analogRead(A0); delay(0); int sensor2 = analogRead(A1); delay(0); Serial.print(sensor); Serial.print(','); Serial.print(sensor2); Serial.print(','); Serial.println(fire); } } }
import processing.serial.*; Serial myPort; int xPos=0; int yPos=0; int button=0; //boolean Button = false; boolean onOff=false; boolean onOff2=false; //Array[] marks; //Mark mark; void setup() { size(960, 720); printArray(Serial.list()); String portname=Serial.list()[1]; println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw() { //background(255); drawTarget(); drawCross(); if (button == 1) { drawMark(); //marks.add(new Mark); } //if (keyPressed){ // drawTarget(); //} } void serialEvent(Serial myPort) { String s=myPort.readStringUntil('\n'); //String t=myPort.readStringUntil('\n'); s=trim(s); //t=trim(t); if (s!=null) { println(s); int values[] = int (split(s, ',')); if (values.length ==3) { xPos=(int)map(values[0], 0, 1023, 0, width); yPos=(int)map(values[1], 0, 1023, 0, height); button= (int) values[2]; } } myPort.write(int(onOff)+","+int(onOff2)+"\n"); } void drawMark() { noStroke(); fill(20, 15, 0); ellipse (xPos,yPos, 10, 10); } void drawCross() { fill(0, 255, 150); noStroke(); rectMode(CENTER); rect(xPos, yPos, 3, 30); rect(xPos, yPos, 30, 3); rect(xPos-20, yPos, 3, 15); rect(xPos+20, yPos, 3, 15); rect(xPos, yPos-20, 15, 3); rect(xPos, yPos+20, 15, 3); } void drawTarget() { fill(200, 118, 70, 240); noStroke(); rectMode(CENTER); rect(width/2, height/2, width, height); fill(240); rect(width/2, height/2, 400, 600); fill(255, 255, 0, 150); rect(width/2, height/2, 300, 450); fill(240); rect(width/2, height/2, 200, 300); fill(255, 255, 0, 150); rect(width/2, height/2, 100, 150); fill(240); rect(width*.17, height*.7, 150, 225); fill(255, 255, 0, 150); rect(width*.17, height*.7, 100, 150); fill(240); rect(width*.17, height*.7, 50, 75); fill(240); rect(width*.83, height*.7, 150, 225); fill(255, 255, 0, 150); rect(width*.83, height*.7, 100, 150); fill(240); rect(width*.83, height*.7, 50, 75); fill(240); rect(width*.17, height*.3, 150, 225); fill(255, 255, 0, 150); rect(width*.17, height*.3, 100, 150); fill(240); rect(width*.17, height*.3, 50, 75); fill(240); rect(width*.83, height*.3, 150, 225); fill(255, 255, 0, 150); rect(width*.83, height*.3, 100, 150); fill(240); rect(width*.83, height*.3, 50, 75); }
At the end, I attempted to make an array list in order to save points on the target that I had shot, but could not figure out how to make it work with my code.
Thanks for reading!