/* ######################## PROGRAM FOR PALO LIGHTSTICK ########################## * HOW IT WORKS : WHEN PIEZO IS CREATE VOLTAGE HIGH ENOUGH, THE ARDUINO WILL SWITCH ON THE MOSFET TYPE N * AND IT WILL LIGHTS ON THE LEDS. WHEN PRESS AGAIN, THE PIEZO WILL SWITCH OFF THE LEDS * AUTOR : LOIC ROCHAT * DATE : 17 JANVIER 2016 * CONTACT : loic.rochat@gmail.com */ int pinPiezo = A6; //pin piezo int pinMOS = 3; //pin transistor MOSFET int mesure = 0; //value piezo int interrupteur = 0; // 0 = lamp off, 1 = lamp on void setup() { // put your setup code here, to run once: pinMode(pinPiezo, INPUT); pinMode(pinMOS, OUTPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: mesure = analogRead(pinPiezo); Serial.println(mesure); /* lights on the lamp */ if(mesure > 50 && interrupteur == 0){ while(analogRead(pinPiezo) > 50){ delay(1); } interrupteur = 1; // leds on digitalWrite(pinMOS, HIGH); Serial.println("lamp on"); delay(500); //to reduce the risk that arduino switch off the leds just after it switches them on } /* switch off the lamp */ if(mesure > 50 && interrupteur == 1){ while(analogRead(pinPiezo) > 50){ delay(1); } interrupteur = 0; // leds allumees digitalWrite(pinMOS, LOW); //on eteint les leds par le mosfet Serial.println("lampe eteinte"); delay(500); // same idea that above } /* do not do anything */ else if(mesure <= 50){ delay(10); } }