Capteurs et actionneurs Arduino, les codes sources

capteur
Voici un petit article qui regroupe 35 codes sources pour mettre en oeuvre les différents capteurs et actionneurs sur Arduino que l’on retrouve sur le net.

Capteur de Temperature:

Connecter le signal vers Arduino sur digital 10, et ouvrir le port moniteur série:

#include
 
/* DS18S20 Temperature chip i/o
 
 */
 
OneWire ds(10); // sur pin 10
 
void setup(void) {
 // initialisation inputs/outputs
 // demarrage port serie
 Serial.begin(9600);
}
 
 
 
void loop(void) {
 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];
 int Temp;
 if ( !ds.search(addr)) {
      //Serial.print("No more addresses.n");
      ds.reset_search();
      return;
 }
 
 Serial.print("R="); //R=28 Not sure what this is
 for( i = 0; i < 8; i++) {
   Serial.print(addr[i], HEX);
   Serial.print(" ");
 }
 
 if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!n");
      return;
 }
 
 if ( addr[0] != 0x28) {
      Serial.print("Device is not a DS18S20 family device.n");
      return;
 }
 
 ds.reset();
 ds.select(addr);
 ds.write(0x44,1);        // start conversion, with parasite power on at the end
 
 delay(1000);    // maybe 750ms is enough, maybe not
 // we might do a ds.depower() here, but the reset will take care of it.
 
 present = ds.reset();
 ds.select(addr);
 ds.write(0xBE);   // Read Scratchpad
 
 Serial.print("P=");
 Serial.print(present,HEX);
 Serial.print(" ");
 for ( i = 0; i < 9; i++) {        // we need 9 bytes
   data[i] = ds.read();
   Serial.print(data[i], HEX);
   Serial.print(" ");
 }
 Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature
 
 Temp=Temp>>4;//divide by 16 to get pure celcius readout
 
  //next line is Fahrenheit conversion
 Temp=Temp*1.8+32; // comment this line out to get celcius
 
 Serial.print("T=");//output the temperature to serial port
 Serial.print(Temp);
   Serial.print(" ");
 
 
 Serial.print(" CRC=");
 Serial.print( OneWire::crc8( data, 8), HEX);
 Serial.println();
}

capteur de choc:

Use the LED that is connected to digital 13, and connect the vibration sensor output to digital pin 3. When the vibration sensor detects vibration, the LED will blink.
Sample Code:
 
int Led=13;//define LED interface
int Shock=3//define vibration sensor interface
int val;//define digital varible val
void setup()
{
pinMode(Led,OUTPUT);//define LED as output 
pinMode(Shock,INPUT);//define shock as input
}
void loop()
{
val=digitalRead(Shock);//
if(val==HIGH)//
{
digitalWrite(Led,LOW);
}
else
{
digitalWrite(Led,HIGH);
}
}



Capteur Magnetique:

Use the LED of digital 13, and connect the Hall magnetic field sensor to digital pin 3, and when there is magnetic shield present, the LED will turn on, otherwise, it will turn off.
 
int Led=13;
int SENSOR=3;
int val;
void setup()
{
pinMode(Led,OUTPUT);
pinMode SENSOR,INPUT);
}
void loop()
{
val=digitalRead(SENSOR);
if(val==HIGH)
{
digitalWrite(Led, HIGH);
}
Else
{
digitalWrite(Led, LOW);
}
}
 



Bouton poussoir:

int Led = 13 ;// define LED Interface
int buttonpin = 10; // define the key switch sensor interface
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT); // define LED as output interface
  pinMode (buttonpin, INPUT); // define the key switch sensor output interface
}
void loop ()
{
  val = digitalRead (buttonpin); // digital interface will be assigned a value of 3 to read val
    if (val == HIGH)  // When the key switch when the sensor detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Emetteur infrarouge:

# Include <IRremote.h>
int RECV_PIN = 11; / / define input pin on Arduino
IRrecv irrecv (RECV_PIN);
decode_results results;
void setup ()
{
Serial.begin (9600);
irrecv.enableIRIn (); / / Start the receiver
}
void loop () {
if (irrecv.decode (& results)) {
Serial.println (results.value, HEX);
irrecv.resume (); / / Receive the next value
}
}
Main emission part of the code:
# Include <IRremote.h>
IRsend irsend;
void setup ()
{
Serial.begin (9600);
}
void loop () {
for (int i = 0; i <50; i + +) {
irsend.sendSony (0xa90, 12); / / Sony TV power code
delay (40);
}
}


module buzzer:

//Example Code for KY-006
 
int buzzer = 8 ;// setting controls the digital IO foot buzzer
void setup ()
{
  pinMode (buzzer, OUTPUT) ;// set the digital IO pin mode, OUTPUT out of Wen
}
void loop ()
{
  unsigned char i, j ;// define variables
  while (1)
  {
    for (i = 0; i <80; i++) // Wen a frequency sound
    {
      digitalWrite (buzzer, HIGH) ;// send voice
      delay (1) ;// Delay 1ms
      digitalWrite (buzzer, LOW) ;// do not send voice
      delay (1) ;// delay ms
    }
    for (i = 0; i <100; i++) // Wen Qie out another frequency sound
    {
      digitalWrite (buzzer, HIGH) ;// send voice
      delay (2) ;// delay 2ms
      digitalWrite (buzzer, LOW) ;// do not send voice
      delay (2) ;// delay 2ms
    }
  }
}


Diode laser:

void setup ()
{
   pinMode (13, OUTPUT); // define the digital output interface 13 feet
}
void loop () {
   digitalWrite (13, HIGH); // open the laser head
   delay (1000); // delay one second
   digitalWrite (13, LOW); // turn off the laser head
   delay (1000); // delay one second
}


Led RGB:

int redpin = 11; // select the pin for the red LED
int bluepin = 10; // select the pin for the blue LED
int greenpin = 9; // select the pin for the green LED
int val=0;
void setup () {
  pinMode (redpin, OUTPUT);
  pinMode (bluepin, OUTPUT);
  pinMode (greenpin, OUTPUT);
  Serial.begin (9600);
}
 
void loop () {
  for (val=255; val>0; val--)
  {
    analogWrite (11, val);
    analogWrite (10, 255-val);
    analogWrite (9, 128-val);
    delay (1);
  }
  for (val = 0; val <255; val++)
  {
    analogWrite (11, val);
    analogWrite (10, 255-val);
    analogWrite (9, 128-val);
    delay (1);
  }
  Serial.println (val, DEC);
}


Capteur optique :

// Example code for KY-010
// photo interrupter module
 
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the photo interrupter sensor interface
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// define the photo interrupter sensor output interface   
}
void loop ()
{
  val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
  if (val == HIGH) // When the light sensor detects a signal is interrupted, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Led bi color:

// Arduino test code for KY011
int redpin = 11; // select the pin for the red LED
int greenpin = 10; // select the pin for the green LED
int val;
void setup () {
   pinMode (redpin, OUTPUT);
   pinMode (greenpin, OUTPUT);
}
void loop () {
   for (val = 255; val> 0; val--)
      {
      analogWrite (greenpin, val);
      analogWrite (redpin, 255-val);
      delay (15);
   }
   for (val = 0; val <255; val++)
      {
      analogWrite (greenpin, val);
      analogWrite (redpin, 255-val);
      delay (15);
   }  
}


Capteur temperature analogique:

#include <math.h>
 
int sensorPin = A5; // select the input pin for the potentiometer
 
double Thermistor(int RawADC) {
  double Temp;
  Temp = log(10000.0*((1024.0/RawADC-1))); 
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius
   //Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
   return Temp;
}
 
void setup() {
 Serial.begin(9600);
}
 
void loop() {
 int readVal=analogRead(sensorPin);
 double temp =  Thermistor(readVal);
 
 Serial.println(temp);  // display tempature
 //Serial.println(readVal);  // display tempature
 
 delay(500);
}


Capteur temperature et humidité:

//KY015 DHT11 Temperature and humidity sensor 
int DHpin = 8;
byte dat [5];
byte read_data () {
  byte data;
  for (int i = 0; i < 8; i ++) {
    if (digitalRead (DHpin) == LOW) {
      while (digitalRead (DHpin) == LOW); // wait for 50us
      delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'
      if (digitalRead (DHpin) == HIGH)
        data |= (1 << (7-i)); // high front and low in the post
      while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver
     }
  }
return data;
}
 
void start_test () {
  digitalWrite (DHpin, LOW); // bus down, send start signal
  delay (30); // delay greater than 18ms, so DHT11 start signal can be detected
 
  digitalWrite (DHpin, HIGH);
  delayMicroseconds (40); // Wait for DHT11 response
 
  pinMode (DHpin, INPUT);
  while (digitalRead (DHpin) == HIGH);
  delayMicroseconds (80); // DHT11 response, pulled the bus 80us
  if (digitalRead (DHpin) == LOW);
  delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data
 
  for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered
    dat[i] = read_data ();
 
  pinMode (DHpin, OUTPUT);
  digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal
}
 
void setup () {
  Serial.begin (9600);
  pinMode (DHpin, OUTPUT);
}
 
void loop () {
  start_test ();
  Serial.print ("Current humdity =");
  Serial.print (dat [0], DEC); // display the humidity-bit integer;
  Serial.print ('.');
  Serial.print (dat [1], DEC); // display the humidity decimal places;
  Serial.println ('%');
  Serial.print ("Current temperature =");
  Serial.print (dat [2], DEC); // display the temperature of integer bits;
  Serial.print ('.');
  Serial.print (dat [3], DEC); // display the temperature of decimal places;
  Serial.println ('C');
  delay (700);
}


Module RGB:

//KY016 3-color LED module
int redpin = 11; // select the pin for the red LED
int bluepin = 10; // select the pin for the blue LED
int greenpin = 9 ;// select the pin for the green LED
int val;
void setup () {
  pinMode (redpin, OUTPUT);
  pinMode (bluepin, OUTPUT);
  pinMode (greenpin, OUTPUT);
  Serial.begin (9600);
}
void loop ()
{
  for (val = 255; val> 0; val --)
  {
    analogWrite (11, val);
    analogWrite (10, 255-val);
    analogWrite (9, 128-val);
    delay (10);
    Serial.println (val, DEC);
  }
  for (val = 0; val <255; val ++)
  {
    analogWrite (11, val);
    analogWrite (10, 255-val);
    analogWrite (9, 128-val);
    delay (10);
    Serial.println (val, DEC);
  }
}


Module contact boule de mercure:

//KY017 Mercury open optical module
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the mercury tilt switch sensor interface
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// define the mercury tilt switch sensor output interface
}
void loop ()
{
  val = digitalRead (buttonpin) ;// read the values assigned to the digital interface 3 val
  if (val == HIGH) // When the mercury tilt switch sensor detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Capteur luminosité:

//KY018 Photo resistor module
 
int sensorPin = A5; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  digitalWrite(ledPin, HIGH);
  delay(sensorValue);
  digitalWrite(ledPin, LOW);
  delay(sensorValue);
  Serial.println(sensorValue, DEC);
}


Sortie relais:

//KY019 5V relay module
int relay = 10; // relay turns trigger signal - active high;
void setup ()
{
  pinMode (relay, OUTPUT); // Define port attribute is output;
}
void loop ()
{
  digitalWrite (relay, HIGH); // relay conduction;
  delay (1000);
  digitalWrite (relay, LOW); // relay switch is turned off;
  delay (1000);
}


Interrupteur Tilt:

int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the tilt switch sensor interfaces
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
    pinMode (buttonpin, INPUT) ;//define the output interface tilt switch sensor
}
void loop ()
{
  val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
    if (val == HIGH) //When the tilt sensor detects a signal when the switch, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Détecteur de led flash:

//
// Example code for sensor KY021
// More info on http://tkkrlab.nl/wiki/Arduino_KY-021_Mini_magnetic_reed_modules
//
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the Reed sensor interfaces
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// output interface as defined Reed sensor
}
void loop ()
{
  val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
  if (val == HIGH) // When the Reed sensor detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Récepteur infrarouge:

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */
 
#include <IRremote.h>
 
int RECV_PIN = 11;
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}
void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
}


Mini joystick 2 axes:

// Module KY023
// For more info see http://tkkrlab.nl/wiki/Arduino_KY-023_XY-axis_joystick_module
int JoyStick_X = A0; // x
int JoyStick_Y = A1; // y
int JoyStick_Z = 3; // key
void setup ()
{
  pinMode (JoyStick_X, INPUT);
  pinMode (JoyStick_Y, INPUT);
  pinMode (JoyStick_Z, INPUT);
  Serial.begin (9600); // 9600 bps
}
void loop ()
{
  int x, y, z;
  x = analogRead (JoyStick_X);
  y = analogRead (JoyStick_Y);
  z = digitalRead (JoyStick_Z);
  Serial.print (x, DEC);
  Serial.print (",");
  Serial.print (y, DEC);
  Serial.print (",");
  Serial.println (z, DEC);
  delay (100);
}


Capteur à effet Hall:

int Led = 13 ;/ / define LED Interface
int buttonpin = 3; / / define the linear Hall magnetic sensor interface
int val ;/ / define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;/ / define LED as output interface
pinMode (buttonpin, INPUT) ;/ / define linear Hall magnetic sensor output interface
}
void loop ()
{
val = digitalRead (buttonpin) ;/ / digital interface will be assigned a value of 3 to read val
if (val == HIGH) / / When the linear Hall sensor detects a magnetic signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}


Détecteur led Flash (BIG):

int Led = 13 ;/ / define LED Interface
int buttonpin = 3; / / define the Reed sensor interfaces
int val ;/ / define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;/ / define LED as output interface
pinMode (buttonpin, INPUT) ;/ / output interface as defined Reed sensor
}
void loop ()
SunFounder{
val = digitalRead (buttonpin) ;/ / digital interface will be assigned a value of 3 to read val
if (val == HIGH) / / When the Reed sensor detects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}


Capteur de flamme:

//Example for KY-026
//TkkrLab
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the flame sensor interface
int analoog = A3; // define the flame sensor interface
 
int val ;// define numeric variables val
float sensor; //read analoog value
 
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// output interface defines the flame sensor
  pinMode (analoog, INPUT) ;// output interface defines the flame sensor
  Serial.begin(9600);
}
 
void loop ()
{
  sensor = analogRead(analoog);
  Serial.println(sensor);  // display tempature
 
  val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
    if (val == HIGH) // When the flame sensor detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
  delay(1000);
}


Led magique:

nt LedPinA = 5;
int LedPinB = 6;
int ButtonPinA = 7;
int ButtonPinB = 4;
int buttonStateA = 0;
int buttonStateB = 0;
int brightness = 0;
void setup ()
{
pinMode (LedPinA, OUTPUT);
pinMode (LedPinB, OUTPUT);
pinMode (ButtonPinA, INPUT);
pinMode (ButtonPinB, INPUT);
}
void loop ()
{
buttonStateA = digitalRead (ButtonPinA);
if (buttonStateA == HIGH && brightness! = 255)
{
brightness + +;
}
buttonStateB = digitalRead (ButtonPinB);
if (buttonStateB == HIGH && brightness! = 0)
{
brightness -;
}
analogWrite (LedPinA, brightness); / / A few Guan Yuan (ii)? analogWrite (LedPinB, 255 - brightness); 
/ / B Yuan (ii) a few Bang? 
Delay (25);
}


Capteur t° digital:

int Led = 13 ;/ / define LED Interface
int buttonpin = 3; / / define the digital temperature sensor interface
int val ;/ / define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;/ / define LED as output interface
pinMode (buttonpin, INPUT) ;/ / define digital temperature sensor output interface
}
void loop ()
{
val = digitalRead (buttonpin) ;/ / digital interface will be assigned a value of 3 to read val
if (val == HIGH) / / when the digital temperature sensor detects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}


Capteur de choc:

int Led = 13 ;/ / define LED Interface
int Shock = 3 / / define the percussion Sensor Interface
int val ;/ / define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;/ / define LED as output interface
pinMode (Shock, INPUT) ;/ / define knock sensor output interface
}
void loop ()
{
val = digitalRead (Shock) ;/ / read digital interface is assigned a value of 3 val
if (val == HIGH) / / When the percussion when the sensor detects a signal, LED flashes
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}
</syntaxhighlight lang="C">


Capteur obstacle:

int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the obstacle avoidance sensor interface
int val ;// define numeric variables val
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// define the obstacle avoidance sensor output interface
}
void loop ()
{
  val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
  if (val == HIGH) // When the obstacle avoidance sensor detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Capteur suiveur de ligne:

int sensorPin = A5; / / select the input pin
int ledPin = 13; / / select the pin for the LED
int sensorValue = 0; / / variable to store the value coming from the
sensor
void setup () {
pinMode (ledPin, OUTPUT);
Serial.begin (9600);
}
void loop () {
sensorValue = analogRead (sensorPin);
digitalWrite (ledPin, HIGH);
delay (sensorValue);
digitalWrite (ledPin, LOW);
delay (sensorValue);
Serial.println (sensorValue, DEC);
}


Led 7 couleurs:

/ *
Blink
Turns on an LED on for two second, then off for two second, repeatedly.
This example code is in the public domain.
* /
void setup () {
/ / Initialize the digital pin as an output.
/ / Pin 13 has an LED connected on most Arduino boards:
pinMode (13, OUTPUT);
}
void loop () {
digitalWrite (13, HIGH); / / set the LED on
delay (2000); / / wait for a second
digitalWrite (13, LOW); / / set the LED off
delay (2000); / / wait for a second
}


Capteur effet Hall:

/* 
KY-035 Hall analog sensor
*/
 
int sensorPin = A5;    // select the input pin
int ledPin = 13;       // select the pin for the LED
int sensorValue = 0;   // variable to store the value coming from the sensor
 
void setup () {
pinMode (ledPin, OUTPUT);
Serial.begin (9600);
}
 
void loop () {
sensorValue = analogRead (sensorPin);
digitalWrite (ledPin, HIGH);
delay (sensorValue);
digitalWrite (ledPin, LOW);
delay (sensorValue);
Serial.println (sensorValue, DEC);
}


Capteur de touche métallique:

int Led = 13 ;/ / define LED Interface
int buttonpin = 3; / / define Metal Touch Sensor Interface
int val ;/ / define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;/ / define LED as output interface
pinMode (buttonpin, INPUT) ;/ / define metal touch sensor output interface
}
void loop ()
{
val = digitalRead (buttonpin) ;/ / digital interface will be assigned a value of 3 to read val
if (val == HIGH) / / When the metal touch sensor detects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}


Capteur micro sensible:

analogique:

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
 
void setup () 
{
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
}
 
void loop () 
{
  sensorValue = analogRead (sensorPin);
  digitalWrite (ledPin, HIGH);
  delay (sensorValue);
  digitalWrite (ledPin, LOW);
  delay (sensorValue);
  Serial.println (sensorValue, DEC);
}

numérique:

int Led = 13 ;// define LED Interface
int buttonpin = 3; // define D0 Sensor Interface
int val = 0;// define numeric variables val
 
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// output interface D0 is defined sensor
}
 
void loop ()
{
  val = digitalRead(buttonpin);// digital interface will be assigned a value of pin 3 to read val
  if (val == HIGH) // When the sound detection module detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Capteur micro:

Analogique:

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
 
void setup () 
{
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
}
 
void loop () 
{
  sensorValue = analogRead (sensorPin);
  digitalWrite (ledPin, HIGH);
  delay (sensorValue);
  digitalWrite (ledPin, LOW);
  delay (sensorValue);
  Serial.println (sensorValue, DEC);
}

Numerique:

int Led = 13 ;// define LED Interface
int buttonpin = 3; // define D0 Sensor Interface
int val = 0;// define numeric variables val
 
void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// output interface D0 is defined sensor
}
 
void loop ()
{
  val = digitalRead(buttonpin);// digital interface will be assigned a value of pin 3 to read val
  if (val == HIGH) // When the sound detection module detects a signal, LED flashes
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}


Capteur heatbeat:

// Pulse Monitor Test Script
int sensorPin = 0;
double alpha = 0.75;
int period = 100;
double change = 0.0;
double minval = 0.0;
void setup ()
{
  Serial.begin (9600);
}
void loop ()
{
    static double oldValue = 0;
    static double oldChange = 0;
 
    int rawValue = analogRead (sensorPin);
    double value = alpha * oldValue + (1 - alpha) * rawValue;
 
    Serial.print (rawValue);
    Serial.print (",");
    Serial.println (value);
    oldValue = value;
 
    delay (period);
}


Encodeur:

int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int aPin = 6;
int bPin = 7;
int buttonPin = 5;
int state = 0;
int longPeriod = 5000; // Time at green or red
int shortPeriod = 700; // Time period when changing
int targetCount = shortPeriod;
int count = 0;
void setup ()
{
  pinMode (aPin, INPUT);
  pinMode (bPin, INPUT);
  pinMode (buttonPin, INPUT);
  pinMode (redPin, OUTPUT);
  pinMode (yellowPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
}
void loop ()
{
  count++;
  if (digitalRead (buttonPin))
  {
    setLights (HIGH, HIGH, HIGH);
  }
  else
  {
    int change = getEncoderTurn ();
    int newPeriod = longPeriod + (change * 1000);
    if (newPeriod  >= 1000 && newPeriod <= 10000)
    {
      longPeriod = newPeriod;
    }
    if (count> targetCount)
    {
      setState ();
      count = 0;
    }
  }
  delay (1);
}
int getEncoderTurn ()
{
  // Return -1, 0, or +1
  static int oldA = LOW;
  static int oldB = LOW;
  int result = 0;
  int newA = digitalRead (aPin);
  int newB = digitalRead (bPin);
  if (newA != oldA || newB != oldB)
  {
    //Something has changed
    if (oldA == LOW && newA == HIGH)
    {
      result = - (oldB * 2 - 1);
    }
  }
  oldA = newA;
  oldB = newB;
  return result;
}
int setState ()
{
  if (state == 0)
  {
    setLights (HIGH, LOW, LOW);
    targetCount = longPeriod;
    state = 1;
  }
  else if (state == 1)
  {
    setLights (HIGH, HIGH, LOW);
    targetCount = shortPeriod;
    state = 2;
  }
  else if (state == 2)
  {
    setLights (LOW, LOW, HIGH);
    targetCount = longPeriod;
    state = 3;
  }
  else if (state == 3)
  {
    setLights (LOW, HIGH, LOW);
    targetCount = shortPeriod;
    state = 0;
  }
}
void setLights (int red, int yellow, int green)
{
  digitalWrite (redPin, red);
  digitalWrite (yellowPin, yellow);
  digitalWrite (greenPin, green);
}

Vous pouvez recevoir un email dès la parution d’un article sur le Blog F8ASB.COM, entrez votre mail sur la fenêtre à droite et cliquer sur abonnement.
Ce contenu a été publié dans arduino, BANGGOOD, electronique, Microcontrolleur, avec comme mot(s)-clé(s) , , , , . Vous pouvez le mettre en favoris avec ce permalien.

Une réponse à Capteurs et actionneurs Arduino, les codes sources

  1. eyraud claude dit :

    trés bien! je débute en arduino.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.