Archives mensuelles : janvier 2016

image_pdfimage_print

Arduino + LCD (2 lines 16 chars)

Parts :

– 1 x Arduino nano

– 1 x LCD

– 1 x Pot 10 kΩ

20151217_150451

// include the library code:
#include

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 1);
// Print a message to the LCD.
lcd.print(« hello, world! »);
}

void loop() {
lcd.setCursor(0, 1);

lcd.print(« hello, world! »);
}

 

Arduino + SR-HC04

Parts :

– 1 x Arduino nano

– 1 x led

– 1 x buzzer

– 1x R 220 Ω

– 1 x SR-HC04 module

int trig = 12;
int echo = 11;
long lecture_echo;
long cm;
int LED = 6;

void setup()
{
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
}

void loop()
{
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
lecture_echo = pulseIn(echo, HIGH);
cm = lecture_echo / 58;
Serial.print(« Distancem : « );
Serial.println(cm);
delay(1000);

if(cm<125)
{
digitalWrite(LED, HIGH);
beep(200);
}
else
{
digitalWrite(LED,LOW);
}
}

void beep(unsigned char delayms){
analogWrite(9, 20); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(9, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}

 

Find the north

boussole_bb

Parts :

– 1 x Arduino nano

– 1 x led

– 1x R 220 Ω

– 1 x HMC5883L module


#include //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883
int LED = 6;
float direction;

void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();

//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
pinMode(LED,OUTPUT);
}

void loop(){

int x,y,z; //triple axis data

//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();

//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){ x = Wire.read()<<8; //X msb x |= Wire.read(); //X lsb z = Wire.read()<<8; //Z msb z |= Wire.read(); //Z lsb y = Wire.read()<<8; //Y msb y |= Wire.read(); //Y lsb } //Print out values of each axis Serial.print("x: "); Serial.print(x); Serial.print(" y: "); Serial.print(y); Serial.print(" z: "); Serial.println(z); if ( y > 0 )
{
direction= 90 - atan2(y,x)*180.0/PI;
}
else
{
direction= 270 - atan2(y,x)*180.0/PI;
}
Serial.print("direction: ");
Serial.print(direction);
delay(250);

if (abs(direction)<20) { digitalWrite(LED, HIGH); Serial.print("Switch on led"); } else { digitalWrite(LED, LOW); } }

Arduino + motion detector + led

Parts :

– 1 x Arduino nano

– 1 x led high lumi

– 1x R 330 Ω

-1 x R 1kΩ

– 1 x Transistor NPN 2N2222

– 1 x HC SR501 PIR motion detector

ledinfra_bb


#include
#include

void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
digitalWrite(2, LOW);
pinMode(5,OUTPUT);

digitalWrite(5,HIGH);
delay(5000);
digitalWrite(5,LOW);
delay(200);
}

void loop()
{
//Come out of sleep and read state of PIR pin
bool flag = digitalRead(2);
if (flag == true)
{
Serial.println("tripped");
digitalWrite(5,HIGH);
}
else
{
Serial.println("not tripped");
digitalWrite(5,LOW);
}
delay(100);
sleepNow();
}

void sleepNow()
{
Serial.println("Entering Sleep");
// Set pin 2 as interrupt and attach handler:
attachInterrupt(digitalPinToInterrupt(2), wakeUp, CHANGE);
delay(100);
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);

// Set sleep enable (SE) bit:
sleep_enable();

// Put the device to sleep:
sleep_mode();

// Upon waking up, sketch continues from this point.
sleep_disable();
}

void wakeUp(void)
{
Serial.println("wake up");
detachInterrupt(digitalPinToInterrupt(2));
}