Lyd Sensor

Sound sensor modulet er en simpel mikrofon baseret på sound amplifier og electret mikrofonen, kan det anvendes til at detekte lyd styrken fra miljøet omkring den.

Den lille lydsensor (mikrofon) tillægger en analog pin på Arduino og kan anvendes til at detekte niveauet af støj i omgivelserne, hvor du potentielt kan bruge sensoren til at tænde et lys i dit hus efter at anerkende en særlig klap / fløjte sekvens.

Figur 1 viser, hvordan tilslutning af lyd sensor til Arduino kan se ud i praksis.

Lyd Sensor
Figur 1 – Lyd Sensor Eksempel 1

Her er et simpelt kode eksempel:

//SOUND Sensor

int sensorPin = 4;    //Microphone Sensor Pin on analog 4
int YellowLED = 1; 
int RedLED = 4; 
int sensorValue = 0;

void setup() {
  pinMode(YellowLED, OUTPUT);
pinMode(RedLED, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin); 
Serial.println(sensorValue);

/* If you feel that the sensor values on the serial monitor
are going by too quickly for you to read, you can make it
so that the values only show up on the serial monitor
if you make a noise.  You can replace
Serial.println(sensorValue);
  with:
if (sensorValue < 509){
   Serial.println(sensorValue);
}

*/

  digitalWrite(YellowLED, HIGH);  //Yellow is always on.

  if (sensorValue>25){  //The 'silence' sensor value is 509-511
    digitalWrite(RedLED,HIGH);
    delay(15 );            // The red LEDs stay on for 2 seconds
} else {
    digitalWrite(RedLED,LOW);
}
}

Et andet eksempel på brug af lyd sensor kunne være nedenstående Figur 2 og Kode:

Lyd Sensor 2
Figur 2 – Lyd Sensor Eksempel 2
int soundSensorPin=A0;
int soundReading=0;
int soundThreshold=500;
int intensity[3]={0,0,0};
int LEDPins[3] = {3,5,6};
int numberOfPins=3;
int currentPin=0;
int fadeCounter=0;
int fadeDelay=50;
boolean switcher = true;

void setup(){
 pinMode(soundSensorPin, INPUT);
 for(int i=0; i<numberOfPins;i++){
 pinMode(LEDPins[i],OUTPUT);
 }
}

void loop(){
 soundReading=analogRead(soundSensorPin);
 if(soundReading>soundThreshold){
 if(switcher){
 aboveThreshold(currentPin);
 switcher=true;
  }
 } else {
 if(switcher){
 belowThreshold();
 switcher=true;
  }
 }
}

void aboveThreshold(int cPin){
 switcher=false;
 if(intensity[cPin]<10){
 intensity[cPin]=255;
 delay(50);
 currentPin=currentPin+1;
 }

 if(currentPin==numberOfPins){
 currentPin=0;
 }
}

void belowThreshold(){
 switcher=false;
 fadeCounter++;
 if(fadeCounter==fadeDelay){
 fadeCounter=0;
 for(int i=0; i<numberOfPins;i++){
 analogWrite(LEDPins[i],intensity[i]);
 }
 for(int i=0; i<numberOfPins;i++){
 intensity[i]--;
 if(intensity[i]<0){
 intensity[i]=0;
   }
  }
 }
}

Og ligeledes er der en video til ovenstående eksempel.

Farve Sensor

En farvesensor er ikke meget anderledes end en digital kamera.

Farvesensorer arbejder efter et princip, hvor tre lyskilder med hhv. rød-grøn-blå lyskilde sendes mod et emne. Det lys, som retuneres, vil have ændret sammensætning afhængigt af emnets farve. Det returnerede lys sammenholdes med et indlært refrenceniveau, og passer dette sammen, afgives et signal.
Der kan også detekteres farve på gennemsigtige emner, på lang afstand, hvor der benyttes en reflektor bag emnet.

Figur 3 viser, hvordan tilslutning af farvesensor til Arduino kan se ud i praksis. Derunder ligger til passende kode:

Color Sensor Arduino
Figur 3 – Color Sensor Arduino 1
/**Runs a color sensor on the Arduino
* Start the program while showing it the white card. When the RGB LED turns off,
* switch (quickly) to the black card. After the RGB LED flashes white, it is ready.
*/
//pin identifiers
int blue = A2;
int green = A1;
int red = A0;
int redOut = 9;
int greenOut = 10;
int blueOut = 11;
//counts the number of loops - used to time events
int count = 0;
//stores the current RGB input
int out[3];
//ICOLOR = RGB input from transistor that is that color.
//COLOR = RGB output to make RGB LED show that color
int IWHITE[3] = {175,48,455}; int WHITE[3] = {224,226,243};
int IBLACK[3] = {42,5,20}; int OFF[3] = {0,0,0};
int IRED[3] = {145,15,38}; int RED[3] = {200,0,0};
int IYELLOW[3] = {175,40,60}; int YELLOW[3] = {255,150,0};
int IMAGENTA[3] = {150,16,83}; int MAGENTA[3] = {200,0,200};
int IBLUE[3] = {57,7,160}; int BLUE[3] = {0,0,200};
int IGREEN[3] = {41,6,50}; int GREEN[3] = {0,200,0};
int NONE[3];
//[0] of these is the min, [1] is the max.
int redConvert[2];
int greenConvert[2];
int blueConvert[2];

//reads in the current color
int* readColor(){
  int ret[3];
  ret[0] = analogRead(red);
  ret[1] = analogRead(green);
  ret[2] = analogRead(blue);
  return ret;
}

//checks that each element in color1 is within dev of
//the corresponding element in refColor
boolean inRange(int color1[], int refColor[], int dev){
  return (abs(color1[0] - refColor[0]) <= dev) &&
  abs(color1[1] - refColor[1]) <= dev &&
  abs(color1[2] - refColor[2]) <= dev;
}

//sets the RGB LED color
void setColor(int color[]){
  analogWrite(redOut, color[0]);
  analogWrite(greenOut, color[1]);
  analogWrite(blueOut, color[2]);
}

//determines values for black and white
void calibrate(){
  setColor(RED);
  delay(500);
  setColor(GREEN);
  delay(500);
  setColor(BLUE);
  delay(500);
  setColor(WHITE);
  int redSum = 0;
  int greenSum = 0;
  int blueSum = 0;
  for(int i = 1; i <= 5; i++){
    redSum += analogRead(red);
    greenSum += analogRead(green);
    blueSum += analogRead(blue);
    delay(333);
  }
  int whiteCal[3] = {redSum/5,greenSum/5,blueSum/5};
  redSum = blueSum = greenSum = 0;
  setColor(OFF);
  Serial.print("white Cal (RGB): ");
  Serial.print(whiteCal[0]);
  Serial.print(" ");
  Serial.print(whiteCal[1]);
  Serial.print(" ");
  Serial.println(whiteCal[2]);
  Serial.println("insert black card");
  while(inRange(readColor(),whiteCal,50)){
    delay(1000);
    Serial.print("red green blue: ");
    Serial.print(analogRead(red));
    Serial.print(" ");
    Serial.print(analogRead(green));
    Serial.print(" ");
    Serial.println(analogRead(blue));
    //setColor(out);
  }
  Serial.println("new card detected - I hope it's black");
  delay(5000);
  for(int i = 1; i <= 5; i++){
    redSum += analogRead(red);
    greenSum += analogRead(green);
    blueSum += analogRead(blue);
    delay(100);
  }
  int blackCal[3] = {redSum/5,greenSum/5,blueSum/5};
  Serial.print("black Cal (RGB): ");
  Serial.print(blackCal[0]);
  Serial.print(" ");
  Serial.print(blackCal[1]);
  Serial.print(" ");
  Serial.println(blackCal[2]);
  //calculate conversion functions
  redConvert[0] = blackCal[0];
  redConvert[1] = whiteCal[0];
  greenConvert[0] = blackCal[1];
  greenConvert[1] = whiteCal[1];
  blueConvert[0] = blackCal[2];
  blueConvert[1] = whiteCal[2];
}

//runs calibration routine and turns the light white before
//starting loop
void setup(){
  Serial.begin(9600);
  calibrate();
  setColor(WHITE);
  delay(1000);
}

void loop(){
  //reads in new values
  out[0] = analogRead(red);//0-1024
  out[1] = analogRead(green);
  out[2] = analogRead(blue);

//update RGB LED and print output less frequently
  if(count % 5000 == 0){
    Serial.print("red green blue: ");
    Serial.print(out[0]);
    Serial.print(" ");
    Serial.print(out[1]);
    Serial.print(" ");
    Serial.println(out[2]);

//comment out the rest of this loop to just display values and not use the RGB LED
    if(inRange(out,IWHITE,10)){
      Serial.println("White!");
      setColor(WHITE);
    }
    else if(inRange(out,IRED,10)){
      Serial.println("Red!");
      setColor(RED);
    }
    else if(inRange(out,IBLUE,10)){
      Serial.println("Blue!");
      setColor(BLUE);
    }
    else if(inRange(out,IYELLOW,10)){
      Serial.println("Yellow!");
      setColor(YELLOW);
    }
    else if(inRange(out,IMAGENTA,10)){
      Serial.println("Magenta!");
      setColor(MAGENTA);
    }
    else if(inRange(out,IGREEN,10)){
      Serial.println("Green!");
      setColor(GREEN);
    }
    else setColor(OFF);
  }
  count++;
}

Figur 4 viser resultatet:

Color Sensor Result
Figur 4 – Color Sensor Result 1

Og ligeledes er der en video til ovenstående eksempel.

Figur 5 viser et andet eksempel, derunder et link til koden:

Arduino Color Sensor 1
Figur 5 – Arduino Color Sensor 2

Link til Kilde/Kode.

 Kilder

Lyd:
Info + Eksempel 1
Eksempel 2

Farve:
Info
Eksempel 1
Eksempel 2

Leave a Reply