Part 1
The circuit is basically the same as the exercise 1 last class. But I change the number of x and y to “draw” the picture in an area (x±100,y±100). I picked color of random pixels in this area and use many rectangles to present this. To show the image on the screen more clearly, I put another screen recording here:
The Arduino part is just the AtoP code. And here is the code in Processing (I didn’t include the function of receiving data):
void setup() { size(1032, 688); background(0); setupSerial(); img = loadImage("star trails.jpg"); } void draw() { img.resize(1032, 688); getSerialData(); printArray(sensorValues); posX = int(map(sensorValues[0],0,1023,0,width)); posY = int(map(sensorValues[1],0,1023,0,height)); for (int i = 0; i<300; i++){ int ranX = int(random(-100,100)); int ranY = int(random(-100,100)); tColor = img.get(posX+ranX,posY+ranY); float alpha = map(dist(posX,posY,posX+ranX,posY+ranY),0,144,0,255); noStroke(); fill(tColor,alpha); rect(posX+ranX,posY+ranY,2,10); } }
Part 2:
In this part, I use 3 LEDs to show the amplitude (loudness) of the sound captured by the microphone on the computer and a servo motor to show the largest one. Green, yellow and red LED in turn represent the low, median and loud sound. And the servo motor could record the loudest sound that is received. I also draw a “dashboard” for this. However, it does not include the exact number of dB.
This is what I’ve built:
The reason I choose servo motor to show the loudest sound received is that I originally use map() function to let servo motor to show the real time amplitude. But the amplitude is changing all the time and there are many interruptive noise in the environment. So the servo motor was rotating all the time, very unstable and interrupting (servo motor itself could produce noice when changing the angle). So I use LEDs instead to show the real time amplitude and use servo motor to record the loudest sound. I could have more LEDs to show the real time amplitude more accurately, here is just an example.
This is the code for Processing (not include serial communication code):
void setup() { size(500, 500); background(0); setupSerial(); microphone = new AudioIn(this, 0); microphone.start(); analysis = new Amplitude(this); analysis.input(microphone); } void draw() { background(0); float volume = analysis.analyze(); values[1] = int(volume*100); float diameter = map(volume, 0, 1, 0, width); circle(width/2, height/2, diameter); if(volume<0.03){ values[0] = 0; }else if(volume<0.1){ values[0] = 1; }else{ values[0] = 2; } sendSerialData(); }
This is the code for Arduino (not include serial communication code):
int Max; #include <Servo.h> Servo myservo1; void setup() { Serial.begin(9600); pinMode(13, OUTPUT); myservo1.attach(9); Max = 0; myservo1.write(180); } void loop() { getSerialData(); if (values[1] > Max){ Max = values[1]; } if (values[0] == 0){ digitalWrite(13,HIGH); analogWrite(10,0); analogWrite(11,0); }else if(values[0] == 1){ digitalWrite(13,LOW); analogWrite(10,255); analogWrite(11,0); }else{ digitalWrite(13,LOW); analogWrite(10,0); analogWrite(11,255); } int angle = int(map(Max,0,3,180,0)); myservo1.write(angle); }
Leave a Reply