Stap 6: De Code
Ja, zoals ik eerder, de code geschreven is constanly mesuring de lichtniveaus en het berekenen van de avrage noemde het ambient,
Maar wanneer een shoot sensing een doorweekt piek in de mesuring neemt
// Lightning Catcher for Arduino <br>// Written by Uria Dubinsky // www.udpic.co.il
#define poten A5 // Potentiometer Analog pin no. 5, for setting sensetivity #define seLight A0 // Light Sensor Analog pin no. 0 #define shoot 10 // Optisolator Digital pin no. 10 for making the shoot
int ambient =0; // integer for the ambient light avrage keeping int sensitiv, lastSens; // sensitiv integer for keeping the sensetivity levels value, lastSens integer for keeping the last light metring value
void setup () { Serial.begin (9600); pinMode (poten, INPUT); // Setting the pins modes pinMode (seLight, INPUT); pinMode (shoot, OUTPUT); digitalWrite (shoot, LOW); // Making sure the shoot pin is LOW ambient = analogRead (seLight); // insert a first ambient value }
void loop () { lastSens = analogRead (seLight); // Analog reading the light sensor if (isSpike (lastSens) == false) { // Compering the ambient value for a spike in the light (through the isSpike function) ambient = ((ambient + lastSens) / 2); // If there is no spike enters the value to the ambient avrage } else { digitalWrite (shoot, HIGH); // If there is a spike in the light making the shoot delay (20); digitalWrite (shoot, LOW); } }
boolean isSpike (int sensRead) { // Function recive the value of the last light mesuring and return true for a spike and false for non sensitiv = analogRead (poten); if (sensRead <= ambient+sensitiv ) return false; else return true; }