Étape 3: codage
#include < NewPing.h >
#include < SoftwareServo.h >
#include < FatReader.h >
#include < SdReader.h >
#include < avr/pgmspace.h >
#include « WaveUtil.h »
#include « WaveHC.h »#define TRIGGER_PIN 7
#define ECHO_PIN 6
#define MAX_DISTANCE 500Carte de SdReader ;
Vol FatVolume ;
FatReader racine ;
FatReader f ;WaveHC vague ;
NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE) ;
SoftwareServo servo1 ;unsigned int pingSpeed = 50 ;
unsigned long pingTimer ;boutons de Byte [] = {A0, A1, A2, A3, A4, A5} ;
#define NUMBUTTONS sizeof(buttons)
int freeRam(void)
{
extern int __bss_end ;
extern int * __brkval ;
int free_memory ;
Si ((int) __brkval == 0) {}
free_memory = ((int) & free_memory)-((int) & __bss_end) ;
}
else {}
free_memory = ((int) & free_memory)-((int) __brkval) ;
}
Return free_memory ;
}Sub sdErrorCheck(void)
{
Si (! card.errorCode()) retourner ;
putstring ("\n\rSD I/O erreur:") ;
Serial.Print(Card.ErrorCode(), HEX) ;
putstring (",") ;
Serial.println(Card.ErrorData(), HEX) ;
while(1) ;
}void setup() {}
Serial.Begin(115200) ;SERVO1.Attach(9) ;
Si (! card.init(true)) {}
Si (! card.init()) {}
putstring_nl ("carte init. a échoué!") ;
sdErrorCheck() ;
while(1) ;
card.partialBlockRead(true) ;uint8_t partie ;
pour (partie = 0; partie < 5; partie ++) {}
Si (vol.init (carte, partie))
rupture ;
}
Si (partie == 5) {}
sdErrorCheck() ;
while(1) ;
}putstring ("partition à l’aide") ;
Serial.Print (partie, DEC) ;
putstring (", est de type FAT") ;
Serial.println(vol.fatType(),DEC) ;
Si (! root.openRoot(vol)) {}
putstring_nl ("Impossible d’ouvrir le répertoire racine!") ;
while(1) ;
}
}
}
}
void loop() {}
Delay(100) ;
unsigned int nous = sonar.ping() ;
Serial.Print ("Ping:") ;
Serial.Print(US / US_ROUNDTRIP_CM) ;
Serial.println("cm") ;
Si ((uS / US_ROUNDTRIP_CM) < 50) {}
SERVO1.Write(160) ;
SoftwareServo::refresh() ;
myservo1.Write(90) ;
}
else {}
SERVO1.Write(0) ;
SoftwareServo::refresh() ;
myservo1.Write(0) ;
}
octet i ;
octet statique jouant = -1 ;
int sensor0 = analogRead (touches [0]) ;
Capteur1 int = analogRead (touches [1]) ;
int sensor2 = analogRead (boutons [2]) ;
sensor3 int = analogRead (boutons [3]) ;
int sensor4 = analogRead (touches [4]) ;
int sensor5 = analogRead (touches [5]) ;
Si (900 > analogRead (touches [0])) {}
Si (playing! = 0) {}
jouer = 0 ;
playfile("0.wav") ;
}
}
Serial.Print ("LDR A1 =") ;
Serial.Print(Sensor1) ;
Si {(analogRead (touches [1]) > 900)
Si (playing! = 0) {}
jouer = 0 ;
playfile("1.wav") ;
}
}
Serial.Print ("LDR A2 =") ;
Serial.Print(sensor2) ;
Si {(analogRead (boutons [2]) > 900)
Si (playing! = 0) {}
jouer = 0 ;
playfile("2.wav") ;
}
}
Serial.Print ("LDR A3 =") ;
Serial.Print(sensor3) ;
Si {(analogRead (boutons [3]) > 150)
Si (playing! = 0) {}
jouer = 0 ;
playfile("3.wav") ;
}
}
Serial.Print ("LDR A4 =") ;
Serial.Print(sensor4) ;
Si {(analogRead (touches [4]) > 150)
Si (playing! = 0) {}
jouer = 0 ;
playfile("4.wav") ;
}
}
Serial.Print ("LDR A5 =") ;
Serial.println(sensor5) ;
Si {(analogRead (touches [5]) > 150)
Si (playing! = 0) {}
jouer = 0 ;
playfile("5.wav") ;
}
}
Si (! wave.isplaying) {}
jouer = -1 ;
}
}
void playcomplete(char *name) {}
playfile(Name) ;
alors que {(wave.isplaying)
}
}
void playfile(char *name) {}
Si {(wave.isplaying)
Wave.Stop() ;
}
Si (! f.open (racine, nom)) {}
putstring ("Impossible d’ouvrir le fichier") ; Serial.Print(Name) ; retour ;
}
Si (! wave.create(f)) {}
putstring_nl ("pas un valide" WAV") ; retour ;
}
Wave.Play() ;
}
Vous devez installer la bibliothèque Softwareservo à votre arduino comme waveshield et Servo utilise le même Timer dans arduino... mais lorsque vous utilisez la bibliothèque de Softwareservo tout cela fonctionne bien.
Heureux bâtiment