Étape 5: code complet
#include < SD.h >La valeur par défaut pour la bibliothèque de carte SD
MOSI = broche 11
MISO = axe de 12
SCLK = PIN 13
Nous avons toujours besoin de mettre la broche CS
int CS_pin = 10 ;
#include < Servo.h >
Servo richtingservo ; servo 1
Servo hoekservo ; servo 2
ldr int = 0; / / analogpin 0
int val ;
int j ;
int posplaats ;
hoek int ;
licht int [38] ;
int ldrhoek [19] ;
een van de tableau 181
int plaats [] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,
54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,
72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,
90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,
106,107,108,109,110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,130,131,132,
133,134,135,136,137,138,139,140,141,142,143,144,145,
146,147,148,149,150,151,152,153,154,155,156,157,158,
159,160,161,162,163,164,165,166,167,168,169,170,171,
172,173,174,175,176,177,178,179,180} ;
int lichthoek [] = {30,31,32,33,34,35,36,37,38,39,40,41,
42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,
59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,
76,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,
94,95,96,97,98,99,100,101,102,103,104,105} ;
installation Sub ()
{
richtingservo.Attach(5) ;
hoekservo.Attach(6) ;
Serial.Begin(9600) ;
richtingservo.Write(0) ;
hoekservo.Write(70) ;
Goupille de CS est une sortie
pinMode (CS_pin, sortie) ;
if (!. SD.begin(CS_pin))
{
Serial.println ("panne de la carte") ;
retour ;
}
Serial.println ("carte Ready") ;
}
void loop ()
{
Si (posplaats! = 0)
{
pour (int t = posplaats; t > 0; t--)
{
richtingservo.Write(t) ;
Serial.Print ("posplaats") ; Serial.println(t) ;
Delay(120) ;
}
}
Val = analogRead(ldr) ;
j = analogRead(ldr) ;
int i = 0 ; Elke keer terug op nul voor licht tableau waarden
servo posietie juist zetten fr licht tableau vullen remplies de waarden
pour (int pos0 = 0; pos0 < = 181 ; pos0 ++)
{
richtingservo.Write(plaats[POS0]) ;
Si (pos0 % 5 == 0) / / pos0 delen porte 5 fr als de repos 0 est dan est op het 0,5,10,15,...
{
Delay(300) ;
Val = analogRead(ldr) ; Meten van de licht waarden
Licht [i] = val ; stokeren waarden Licht dans tableau
Serial.Print ("ldr") ; Serial.Print(i) ; Serial.Print("") ; Serial.println(Val) ;
i ++ ;
}
Si (richtingservo.read()==180)
{break;}
Delay(170) ;
}
richtingservo.Write(180) ;
Val = analogRead(ldr) ;
j = analogRead(ldr) ;
Grootste licht waarden zoeken en dans j plaatsen
pour (int a = 0; a < 37 ; un ++)
{
Si (licht [a] < = j)
{
j = licht [a] ;
posplaats = a ;
posplaats = posplaats * 5 ;
Serial.println(posplaats) ;
}}
richtingservo.Write(180) ;
pour (int e = 180; e > posplaats; e--)
{
richtingservo.Write(plaats[e]) ;
Delay(120) ;
}
jmkhhhhhhhhhhhhhhhhhhhjkujkjfiejfknjdfq kmdsjf
Val = analogRead(ldr) ;
j = analogRead(ldr) ;
int y = 0 ;
pour (pos1 int = 0; pos1 < 75 ; pos1 ++)
{
hoekservo.Write(lichthoek[POS1]) ; servo zeten op de plaat voor licht waarden te meten
Si (pos1 % 5 == 0)
{
Delay(300) ;
Val = analogRead(ldr) ; Meten van de licht waarden
ldrhoek [y] = val ; stokeren waarden Licht dans tableau
Serial.Print ("ldr hoek") ; Serial.Print(y) ; Serial.Print("") ; Serial.println(Val) ;
y ++ ;
}
Si (richtingservo.read()==105)
{break;}
Delay(200) ;
}
pour (int z = 0; z < 15; z ++)
{
Si (ldrhoek [z] < = j)
{
j = ldrhoek [z] ;
Hoek = z ;
Hoek = hoek * 5 ;
Serial.println(Hoek) ;
}}
hoekservo.Write(105) ;
hoekservo.Write(lichthoek[Hoek]) ;
Fichier dataFile = SD.open ("log.txt", FILE_WRITE) ;
Si (dataFile)
{
données int = analogRead(0) ;
dataFile.println(data) ;
dataFile.close() ;
Serial.println(Data) ;
}
d’autre
{
Serial.println ("Impossible d’ouvrir le fichier journal") ;
}
Delay(5000) ;
}