Hey Y’all Below is the Code for both Arduino and Process for a Simple Button that spawns a bubble while the wiper from the motor dictates the speed/direction the bubble is flung against the imaginary walls.
Please Feel Free to Ask Any Questions!
-JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include <Servo.h> Servo myservo; //MyServo float mapServo; // Initialized Mapping Position for Servo int sensor = 0; //sensor int pos = 0; //Initialize the button press sensor int inByte = 0; // incoming serial byte void setup() { // start serial port at 9600 bps and wait for port to open: Serial.begin(9600); //Start Serial Conncetion myservo.attach(9); //Activate Count pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { pos=sweep(pos); if(pos == -1) pos =0; // get incoming byte: inByte = Serial.read(); // read first input coming from pin mapServo = map(pos,0,180,0,255); // read switch, map it to 0 or 255L sensor = map(digitalRead(2), 0, 1, 0, 255); // send sensor values: Serial.print(mapServo); Serial.print(","); Serial.println(sensor); if (sensor==255)delay(2500); } } void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0"); // send an initial string delay(300); } } int sweep(int pos){ for(pos; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' if(digitalRead(2) == HIGH) return pos; delay(5); // waits 15ms for the servo to reach the position } for(pos; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' if(digitalRead(2) == HIGH) return pos; delay(5); // waits 15ms for the servo to reach the position } return pos; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
PFont f = createFont("Helvetica",32,true); import processing.serial.*; // import the Processing serial library Serial myPort; // The serial port final float g= 9.807; final float time_to_run = 10000; final int ground_offset=100; boolean finished = false; boolean start_drop =false; int i=0; float xDir = 1; //ball[] c = new ball[2]; ArrayList <ball> c= new ArrayList<ball>(); void setup(){ size(800,600,P3D);//Includes 3D for Sphere // List all the available serial ports println(Serial.list()); myPort = new Serial(this, Serial.list()[7], 9600); // read bytes into a buffer until you get a linefeed (ASCII 10): myPort.bufferUntil('\n'); //Start Fun } void draw(){ if(!start_drop){} else{ background(255); for(ball myBall:c){ myBall.run(); } } } void keyPressed(){ if(key == ' '){ //else{ c.remove(0); //} } } void serialEvent(Serial myPort) { // make sure we have a valid byte if (myPort == null) { return; } // read the serial buffer: String myString = myPort.readStringUntil('\n'); // if you got any bytes other than the linefeed: myString = trim(myString); // split the string at the commas // and convert the sections into integers: float sensors[] = float(split(myString, ',')); if (sensors[0] > 0) { xDir = map(sensors[0], 0, 255, -5, 5); if(xDir == 0) xDir = 1; if (!start_drop && sensors[1] ==255){ c.add(new ball(xDir)); start_drop = true; } if (sensors[1] == 255) { c.add(new ball(xDir)); } // print out the values you got: print("xDir : " + xDir + "\t"); if(sensors[1] == 255) print("SPAWN BALL"); else print("NO SPAWN BALL"); // add a linefeed after all the sensor values are printed: println(); } // send a byte to ask for more data: myPort.write("A"); } class ball{ float iniX = random(100,width); float iniY = random(150,height/2); float randZ= random(100,300); float xPos = iniX; float yPos = iniY; float xDir = 1; float yDir = 1; int curMinHeight = 0; float time; float initime = millis(); float time_of_run = time_to_run+initime; float speedconstraint; int time_check = 0; boolean lets_go = false; float yUpdate; float maxSpeed=0; boolean g_over = false; ball(float _xDir){ xDir = _xDir; } void run(){ drawBall();//Draw Ball updateBall();//Update Ball Position yUpdate = gravityBall(yPos,yDir);//Changes Speed of Ball yDir = yUpdate; checkBounds();//Sees where Ball is stopball(); } void drawBall(){ // background(#000000); //fill(#EF5328);//Basketball Orange //fill(color(random(0,255),random(0,255),random(0,255)));//Rainbowsnake fill(#837242); noStroke(); smooth(); lights();//For 3D pushMatrix(); translate(xPos,yPos,randZ);// Pust Ball in Correct Position sphere(5); popMatrix(); } void updateBall(){ xPos = xPos+xDir; yPos = yPos+yDir; } void checkBounds(){ if(xPos> width ){ xDir = xDir*-1;// Change Direction of Ball } if (xPos<0){ xDir = xDir*-1;//Change Direction of Sign } if (yPos> height-ground_offset){ yDir = yDir*-1; //change Direction of Sign time = millis( )-initime;//Updates Time maxSpeed= yDir+maxSpeed; } if (yPos<curMinHeight){//Lowers the Ceiling so Ball Drops Better yDir = yDir*-1; // change direction of sign } } float gravityBall( float y, float yd){ float temp; speedconstraint = abs((((time_of_run*10)-time)/(time_of_run*10)));// Arbitrary Rate for Ball to Slow Down if(lets_go){// temp = (.1* sqrt(2*g*(millis()-initime-time))) * speedconstraint;//Basically Uses the Distance Traveled Since Hitting the Bottom to Adjust Speed } else temp = (.1* sqrt(2*g*(millis()-initime-time))) * speedconstraint*((time/time_of_run)+1);//Same as Above but Increases Speed as the Ceiling Gets Lower if(yd<0) temp = temp*-1;//Makes sure is not NAN return temp; } void stopball(){ curMinHeight = round((time/time_of_run)*height)-round(maxSpeed);//round((time/time_of_run)*height);//Current Ceiling Height if (curMinHeight>=height/5) lets_go=true;//Tells the Ball to Speed Up if(curMinHeight>=height-ground_offset){//Stops the Ball yPos= height-ground_offset; yDir = 0; xDir = 0; g_over =true; //Tells How Far the Ball has Traveled // println("Starting Position:" , iniX , iniY); // println("Ending Position:" , xPos, yPos); // println("X Distance Traveled", abs(iniX-xPos)); } } //boolean explodeBall(ball other){ // float d = dist(xPos,yPos,other.xPos,other.yPos); // if( d<10){ // return true; // } //} } |