Show Posts
Pages: 1 2 [3] 4 5 ... 13
31  Using Arduino / Sensors / Re: better volume change detection on: December 03, 2013, 02:06:14 pm
you might need a logarithmic scale?


when I print out the output of the mic it makes me think its a problem with the sensitivity of the mic, or my setup. It's mostly that it doesn't change values with ambient sound, works fine up close.
32  Using Arduino / Sensors / Re: better volume change detection on: December 03, 2013, 01:48:54 pm
oops, I just moved it, guess it doesn't like that.

https://www.dropbox.com/s/rxj1lll37r8tth9/arduino-micTestWithHardwareOnly.mov
33  Using Arduino / Sensors / Re: Capacitive Touch Piano on: December 02, 2013, 03:45:52 pm
I posted this in the project guidance forum, but I wanted to post it here to as this sub-forum is more specific to my issue. I hope that is ok.

I am in need of some help. I am trying to build a capacitive touch piano, like the one: http://www.instructables.com/id/Capacitive-Touch-Arduino-Keyboard-Piano/?ALLSTEPS.

I have been trying for hours to get this thing to work, but I cannot for the life of me get it to. I have followed the instructions to a tee, but I cannot get the capactive touch part of this project to work. I have a 10M ohm resistor connected to pin 2 and then in parallel with a wire connected to pin 4 (could be any between 3 and 11). I connected the other ends to a a piece of aluminum foil to act as the "key," however it just does not work correctly. Whenever I touch the foil, nothing happens. The speaker does work sometime though, but it is extremely inconsistent on when it does. I feel like there is an issue with the sensitivity somewhere.

Here is the code I am using:
Code:
/*
  Capacitive-Touch Arduino Keyboard Piano
 
  Plays piano tones through a buzzer when the user taps touch-sensitive piano "keys"
*/

#include <CapacitiveSensor.h>
#include "pitches.h"

#define COMMON_PIN      2    // The common 'send' pin for all keys
#define BUZZER_PIN      A4   // The output pin for the piezo buzzer
#define NUM_OF_SAMPLES  10   // Higher number whens more delay but more consistent readings
#define CAP_THRESHOLD   100  // Capactive reading that triggers a note (adjust to fit your needs)
#define NUM_OF_KEYS     8    // Number of keys that are on the keyboard

// This macro creates a capacitance "key" sensor object for each key on the piano keyboard:
#define CS(Y) CapacitiveSensor(2, Y)

// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; // C-Major scale
//int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale
//int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale

// Defines the pins that the keys are connected to:
CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9), CS(10)};

void setup() {
  // Turn off autocalibrate on all channels:
  for(int i=0; i<8; ++i) {
    keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);
  }
  // Set the buzzer as an output:
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {   
  // Loop through each key:
  for (int i = 0; i < 8; ++i) {
    // If the capacitance reading is greater than the threshold, play a note:
    if(keys[i].capacitiveSensor(NUM_OF_SAMPLES) > CAP_THRESHOLD) {
      tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed
    }
  }
}

I have tried modifying CAP_THRESHOLD as well as NUM_OF_SAMPLES, but that did not seem to change anything.
Also, here is a picture of what I have: http://imgur.com/gmVeh7g

Let me know if you need more information. I am getting extremely frustrated with this right now.
Thanks for the help!

try narrowing the problem down. Have you tried to see if you are even triggering the keys or if its an issue with the speaker? I would try some Serial printing to see if the keys are being triggered:

Code:
void loop() {   
  // Loop through each key:
  for (int i = 0; i < 8; ++i) {
    // If the capacitance reading is greater than the threshold, play a note:
    if(keys[i].capacitiveSensor(NUM_OF_SAMPLES) > CAP_THRESHOLD) {
      tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed
      Serial.print("key "); Serial.print(i); Serial.println(" has been triggered");
    }
  }
}

if you do not see anything happen try lowering:

Code:
#define CAP_THRESHOLD   100

by 10 until you get a trigger. Also monitoring what:

Code:
keys[i].capacitiveSensor(NUM_OF_SAMPLES)

is at is a good idea too, so you can see what the value actually is. Capacitance will definitely be different than his, since cutting the tinfoil slightly smaller will reduce the resistance.
34  Using Arduino / Sensors / better volume change detection on: December 02, 2013, 03:24:03 pm
I am currently using this microphone http://www.amazon.com/gp/product/B00BOM93VK/ref=oh_details_o08_s01_i00?ie=UTF8&psc=1 and have also tried this circuit http://www.instructables.com/id/Arduino-Audio-Input/

however it seems you have to be very close to the microphone for it to detect volume changes. I was hoping with a larger mic it would have higher resolution from farther away, but no luck.  I have also tried adjusting the pot with no luck, it increases the threshold but not the sensitivity.

this was the sketch I was using for the UV meter:

Code:
#define NUMREADINGS 5 // Number of readings (increase for a slow curve)
#define MINSOUND 25   // Min sound intensity
#define MAXSOUND 125  // Max sound intensity
 
const int firstLED = 2;
const int lastLED = 9;
int leds;
int readings[NUMREADINGS];
int index = 0;
int total = 0;
int average = 0;
int sound = 0;
 
int x = 0;
int y = 0;
 
void setup() {
    for (int r = 0; r < NUMREADINGS; r++) {
        readings[r] = 0;
    }
    for (int p = firstLED; p <= lastLED; p++) {
        pinMode(p, OUTPUT);
    }
    leds = (lastLED - firstLED) + 1;
}
 
void loop() {
    total -= readings[index];
    readings[index] = analogRead(0);
    total += readings[index];
    index++;
 
    if (index >= NUMREADINGS) {
       index = 0;
    }
 
    average = abs((total / NUMREADINGS) - 338); // ((1024 * 300) / 1000) = ~338
    sound = map(average, MINSOUND, MAXSOUND, 0, leds);
 
    for (int ledON = firstLED; ledON < (firstLED + sound); ledON++) {
        digitalWrite(ledON, HIGH);
    }
 
    for (int ledOFF = (firstLED + sound); ledOFF <= lastLED; ledOFF++) {
        digitalWrite(ledOFF, LOW);
    }
 
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
}

I made a quick video to show what is happening too: 

EDIT** (link updated): https://www.dropbox.com/s/rxj1lll37r8tth9/arduino-micTestWithHardwareOnly.mov

any suggestions on what I might do to increase the sensitivity of the microphone?
35  Using Arduino / Sensors / Re: best "human" sensing sensor on: November 22, 2013, 03:09:28 pm
There are IR sensors for burglar alarms that will "sense"  anyone in the same room.   Get one of those,  they cost about $3.


I have a few of those, unfortunately the angle of view is a bit high for me. I want it to have about a 45 degree resolution so I can have 8 positions. Maybe there are different kinds I haven't found, are you talking about the PIR ones?

and jremington thank you for the link, the photocell one looks interesting. I will have to try it, I have a few laying around. I rather not have to use CV since I want to be stand alone, however RPI might be possibility too.
36  Using Arduino / Sensors / best "human" sensing sensor on: November 20, 2013, 10:48:06 am
So I am using a sonic sensor at the moment and it is a bit iffy detecting people. I am assuming it has to do with the clothing diffusing the ping too much and it doesn't get back to the sensor. I also tried a IR sensor, but these are pricey and the range is very small (2.5ft or so) so they aren't ideal since I would like at least 5ft of range.

Can anyone give me some suggestions on what I should use?
37  Using Arduino / Programming Questions / Re: custom class that calls a library error on: November 15, 2013, 10:00:29 am
Quote
how you call core arduino libraries:
AF_Motor is NOT a core Arduino library.

Quote
the files are .ino
Why? Try, just for giggles, actually doing it right, and naming the class implementation file with a .cpp extension.

sorry, misspoke. when I said core library I meant library in the arduino/library folder vs a class file relative to the project. I rather have it with the project since these classes wont be reused.

I found the problem, it was kind of silly. Just forgot a semicolon after the class in the class file.
38  Using Arduino / Programming Questions / Re: custom class that calls a library error on: November 15, 2013, 09:41:21 am
The motionTracker.cpp (I hope that's what the last file is actually called) is compiled separately from the sketch. When that file is compiled, the compiler has no clue what a NewPing is. You MUST include NewPing.h in the header file for the class.

Quote
I am just writing a class for a file, not a library ...
The distinction being? There is none. Not a one.

I mentioned earlier I had added it, but it made no difference. Unfortunately, I still get the the same error. the files are .ino, but essentially the same as .cpp

Quote
The distinction being? There is none. Not a one.

actually there is one in how you call the class and its location, my class file being local to the project directory is called. The distinction it initially got me at first:
#include "motionTracker.h"

vs. how you call core arduino libraries:
#include <AFMotor.h>
39  Using Arduino / Programming Questions / Re: custom class that calls a library error on: November 14, 2013, 07:03:54 pm
Code:
motionTracker tracker = motionTracker();
Wrong. You never invoke the constructor explicitly.

Code:
motionTracker tracker;
The constructor will be implicitly called.

Code:
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
What is a NewPing? You don't have any #include statements that define what a NewPing is.

Code:
   motionTracker(){}
Implementation does NOT go in the header file.



NewPing is defined in NewPing.h which is included in main section. I have also tried to include it in the header file, but this didn't work either. 

Quote
Implementation does NOT go in the header file.

ok I have read http://arduino.cc/en/Hacking/LibraryTutorial which seems to be similar to what I am doing, besides the fact I am just writing a class for a file, not a library and fixed a few things. not sure if this is more correct though:

Code:
#include <NewPing.h>
#include <AFMotor.h>
#include "motionTracker.h"

AF_Stepper motor(48, 2);

const int buttonPin = 52;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0; 

motionTracker tracker;

void setup() {
  motor.setSpeed(200);  // 10 rpm   
 
  delay(1000);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  while(!isResetPressed()){
    motor.step(1, BACKWARD, SINGLE);
  }
  motor.step(4, FORWARD, SINGLE);
  motor.release();
}

void loop() {
  delay(50);      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  tracker.checkActiveSensor();
}

void checkButtonState(){
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
       Serial.write("high");
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
      Serial.write("low");
  }
}

boolean isResetPressed(){
   buttonState = digitalRead(buttonPin);
   if (buttonState == LOW) {
    Serial.println("low");
     return false;
   }
   else{ 
     Serial.println("high");
      return true;
   }
}

motionTrackher.h:

Code:
#ifndef MOTIONTRACKER_H
#define MOTIONTRACKER_H

class motionTracker {
  public:
    motionTracker();
    void setSensorValues();
    void checkActiveSensor();
    void moveToActivePositon();
  private:
    #define TRIGGER_PIN  22  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     23  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define TRIGGER_PIN_TWO  24  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN_TWO     25  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    NewPing sonarTwo(TRIGGER_PIN_TWO, ECHO_PIN_TWO, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    int triggeredSensor;
    int currentPostion;
   
    int sensorNinty;
    int sensorOneEighty;
    //int sensorTwoSeventy;
    //int sensorThreesixty;
}

#endif

motionTracker:

Code:
#include "motionTracker.h"

void motionTracker::motionTracker(){
 // the contructor if I eventually need it
}
 
void motionTracker::setSensorValues(){
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  sensorNinty = uS / US_ROUNDTRIP_CM;
  Serial.print("sensor one");
  Serial.println(sensorNinty);
  delay(100);
  unsigned int uSTwo = sonarTwo.ping(); // Send ping, get ping time in microseconds (uS).
  sensorOneEighty = uSTwo / US_ROUNDTRIP_CM; 
  Serial.print("sensor two:");
  Serial.println(sensorOneEighty);
  delay(100);
  //set sensor 3
  //set sensor 4
}

void motionTracker::checkActiveSensor(){
  setSensorValues();
  if(sensorNinty > 0){
    triggeredSensor = 100;       
    moveToActivePositon();
  }
  if(sensorOneEighty > 0){
    triggeredSensor = 150;       
    moveToActivePositon();
  }
}

void motionTracker::moveToActivePositon() {
  if(triggeredSensor == currentPostion){
    return;
  }
  if(triggeredSensor > currentPostion){
    int difference = triggeredSensor - currentPostion;
    motor.step(difference, FORWARD, SINGLE);
    currentPostion =  triggeredSensor; // set active current Position
  }
  else {
    int difference = currentPostion - triggeredSensor;
    motor.step(difference, BACKWARD, SINGLE);
    currentPostion =  triggeredSensor; // set active current Position
  }
}
40  Using Arduino / Programming Questions / custom class that calls a library error on: November 14, 2013, 06:19:41 pm
I am trying to put some of my code into a class to organize it but am getting a strange error that I can't seem to solve

Code:
"expected identifier before numeric constant:

In file included from sketch_nov14a_withCLass.ino:3:
motionTracker.h:13: error: expected identifier before numeric constant
motionTracker.h:13: error: expected ',' or '...' before numeric constant
motionTracker.h:14: error: expected identifier before numeric constant
motionTracker.h:14: error: expected ',' or '...' before numeric constant
motionTracker.h: In member function 'void motionTracker::setSensorValues()':
motionTracker.h:26: error: '((motionTracker*)this)->motionTracker::sonar' does not have class type
motionTracker.h:31: error: '((motionTracker*)this)->motionTracker::sonarTwo' does not have class type
motionTracker.h: In member function 'void motionTracker::moveToActivePositon()':
motionTracker.h:58: error: 'motor' was not declared in this scope
motionTracker.h:63: error: 'motor' was not declared in this scope
sketch_nov14a_withCLass.ino: At global scope:
sketch_nov14a_withCLass:5: error: new types may not be defined in a return type
sketch_nov14a_withCLass.ino:5: note: (perhaps a semicolon is missing after the definition of 'motionTracker')
sketch_nov14a_withCLass:5: error: two or more data types in declaration of 'setup'

here is the code:

Code:
#include <NewPing.h>
#include <AFMotor.h>
#include "motionTracker.h"
AF_Stepper motor(48, 2);

const int buttonPin = 52;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0; 

motionTracker tracker = motionTracker();

void setup() {
  motor.setSpeed(200);  // 10 rpm   
 
  delay(1000);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  while(!isResetPressed()){
    motor.step(1, BACKWARD, SINGLE);
  }
  motor.step(4, FORWARD, SINGLE);
  motor.release();
}

void loop() {
  delay(50);      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  tracker.checkActiveSensor();
}

void checkButtonState(){
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
       Serial.write("high");
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
      Serial.write("low");
  }
}

boolean isResetPressed(){
   buttonState = digitalRead(buttonPin);
   if (buttonState == LOW) {
    Serial.println("low");
     return false;
   }
   else{ 
     Serial.println("high");
      return true;
   }
}

motionTracker.h:

Code:
class motionTracker {

  public:
    #define TRIGGER_PIN  22  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     23  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define TRIGGER_PIN_TWO  24  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN_TWO     25  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    NewPing sonarTwo(TRIGGER_PIN_TWO, ECHO_PIN_TWO, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    int triggeredSensor;
    int currentPostion;
   
    int sensorNinty;
    int sensorOneEighty;
    //int sensorTwoSeventy;
    //int sensorThreesixty;
   
   motionTracker(){}
 
    void setSensorValues(){
      unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
      sensorNinty = uS / US_ROUNDTRIP_CM;
      Serial.print("sensor one");
      Serial.println(sensorNinty);
      delay(100);
      unsigned int uSTwo = sonarTwo.ping(); // Send ping, get ping time in microseconds (uS).
      sensorOneEighty = uSTwo / US_ROUNDTRIP_CM; 
      Serial.print("sensor two:");
      Serial.println(sensorOneEighty);
      delay(100);
      //set sensor 3
      //set sensor 4
    }
   
      void checkActiveSensor(){
      setSensorValues();
      if(sensorNinty > 0){
        triggeredSensor = 100;       
        moveToActivePositon();
      }
      if(sensorOneEighty > 0){
        triggeredSensor = 150;       
        moveToActivePositon();
      }
    }
   
    void moveToActivePositon() {
      if(triggeredSensor == currentPostion){
        return;
      }
      if(triggeredSensor > currentPostion){
        int difference = triggeredSensor - currentPostion;
        motor.step(difference, FORWARD, SINGLE);
        currentPostion =  triggeredSensor; // set active current Position
      }
      else {
        int difference = currentPostion - triggeredSensor;
        motor.step(difference, BACKWARD, SINGLE);
        currentPostion =  triggeredSensor; // set active current Position
      }
    }
}
41  Using Arduino / Sensors / KY-037 microphone sensitivity on: November 14, 2013, 01:04:46 pm
Im using a  KY-037 microphone, and want to try to get it to be more sensitive, enough to read talking in a room. Is this possible with a sensor like this? It seems like I have to make very loud noises for it to change values. I have also tried to adjust the pot that is built in, but not exactly sure how it works since I can continue to turn it, but it does seem to affect the threshold. I am using a basic sketch:


Code:
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  Serial.println(sensorValue);
  delay(100);  
}
42  Using Arduino / Programming Questions / Re: creating an averaging function correctly on: November 07, 2013, 11:25:30 am
from performance point of view
Code:
average = ((9 * average) + newValue + 5) / 10;
the divider better be a multiple of 2  (even if average and newvalue are floats)
Code:
average = ((7 * average) + newValue + 5) / 8;

another boxcar variation (without loop)
Code:
const int count = 4;
int boxcar[count];

int average(int value)
{
  static int i;
  static long sum=0;

  sum -= boxcar[i];  //remove oldest value from sum
  boxcar[i] = value;  // add new value to array
  sum += boxcar[i]; // add new value to sum

  i++;
  if (i == count) i=0;
  return sum/count;
}

Did some related code in the past, might be worth studying.
statistics - http://playground.arduino.cc/Main/Statistics -
running average - http://playground.arduino.cc/Main/RunningAverage -
running median - http://playground.arduino.cc/Main/RunningMedian -


those links were exactly what I was looking for. thank you all for the example sketches too.
43  Using Arduino / Programming Questions / creating an averaging function correctly on: November 05, 2013, 11:50:27 am
Trying to write my own averaging function, but not sure if I am going about it right. I am getting values from processing and sending them to arduino. Here is what I have so far, but It doesn't seem to be working correctly, the math is easy I just am not sure how I should be storing/collecting values.

Code:
import ddf.minim.*;
import processing.serial.*;
Serial comPort;

Minim minim;
AudioInput in;

int lf = 10;

void setup()
{
  int width = 150;
  int height = 500;
  size(width,height, P3D);
  minim = new Minim(this);
  in = minim.getLineIn();
  comPort = new Serial(this, Serial.list()[4], 9600);
}

void draw()
{
  int maxLevel = 650;
 
  int currentLevel = int(in.left.level()*1000);
  int convertToDegrees = int(map(currentLevel, 0, maxLevel, 0, 180));
  smooth();
  background(0);
  stroke(166, 212, 255);
  fill(76, 169, 255);
  rect(0, height, height, -currentLevel);
 
  int avgVal = mean(currentLevel);
  print("current lvl:");
  println(currentLevel);
 
  print("avg lvl:");
  println(avgVal);
  comPort.write(avgVal);
}

int count = 0;
int numberReadings = 2;
int avg;

public int mean(int in){
  count++;
  if(count<numberReadings){
   avg += in;
   avg = avg/count;
  }
  if(count >= numberReadings){
    count = 0;
    avg = in;
  }
  return avg;
}
44  Using Arduino / Programming Questions / Re: getting two servos to rotate exactly opposite of each other on: October 29, 2013, 01:44:05 pm
Assuming that the range of numbers going to the servo is 0-180 couldn't you:
Code:
myservo.write(num);
    myservoTwo.write(180-num);

As it is the code is writing a negative number to the servo.
Most hobby servos that I have used don't even get a full 180°.
If you really want to synchronize them I would suggest steppers.

ahh, subtracting 180 from the num val make them sync, exactly what I needed. I am pretty sure this will go a full 380 since I can make them spin continuously in a circle.
45  Using Arduino / Programming Questions / getting two servos to rotate exactly opposite of each other on: October 29, 2013, 12:36:00 pm
I have an input coming from processing reading audio level and I need two servos to move exactly opposite of each other. They seem to work, but aren't exactly in sync, they will be opposite phase like I like, and generally moving together but sometimes one is delayed not moving at same angle.

Ideally I would like them to move in a full 360, right now they seem to be moving only 180 and anything over seems to make it go crazy, so some insight on that would be great too. here is the code:

processing

Code:
import ddf.minim.*;
import processing.serial.*;
Serial comPort;

Minim minim;
AudioInput in;

int lf = 10;

void setup()
{
  int width = 150;
  int height = 500;
  size(width,height, P3D);
  minim = new Minim(this);
  in = minim.getLineIn();
  // uncomment this line to *hear* what is being monitored, in addition to seeing it
  //in.enableMonitoring();
  frameRate(10);
  comPort = new Serial(this, Serial.list()[4], 9600);
}

void draw()
{
  int maxLevel = 600;
 
  int currentLevel = int(in.left.level()*1000);
  int convertToDegrees = int(map(currentLevel, 0, maxLevel, 0, 360));
  smooth();
  background(0);
  stroke(166, 212, 255);
  fill(76, 169, 255);
  rect(0, height, height, -currentLevel);
 
  line( width/2, height/2, width/2+cos(convertToDegrees)*20, height/2+sin(convertToDegrees)*20);
  ellipse(width/2+cos(convertToDegrees)*20, height/2+sin(convertToDegrees)*20, 10, 10);
 
 
  arc(width/2, 55, 100, 100, PI, sin(radians(convertToDegrees)) );
 
  comPort.write(currentLevel);
  println(currentLevel);
}

arduino:
Code:
#include <Servo.h>

int incomingByte = 0;   
Servo myservo;
Servo myservoTwo;
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object
  myservoTwo.attach(9);
}
 
void loop()
{
  while (Serial.available() > 0) {
    int num=Serial.read()-'0';
    myservo.write(num);
    myservoTwo.write(-num);
 }
}
Pages: 1 2 [3] 4 5 ... 13