Hello
I am making a device that when the user puts their hand less than 20 cm away from either Ultrasonic sensor an LED lights up and music starts playing from an SD card as a wav file, then after a certain amount of time the music stops and the LED switches off.
I have coded each individual bit successfully but when i put it all together it just lists a load of errors.
Attached is the code I am trying to debug but i am completely stuck. any help would be much appreciated.
Thanks
Main_Code_v1.ino (1.4 KB)
Please do us all a favour and post your code here using code tags when you do
Please read How to use this forum
{
void SonarSensor(int trigPin, int echoPin)
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
etc, etc
This is not how you write a function
Compare it with the loop() function
void loop()
{
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
etc, etc
#include <pcmRF.h>
#include <pcmConfig.h>
#include <TMRpcm.h>
#include <SPI.h>
#include <SD.h>
// pin 10 CS
// pin 13 SCK
// pin 11 MOSI
// pin 12 MISO
// Vcc 5v
// Gnd to ground
// pin 9 Speaker
// pin 8 and 7 US-1
// pin 6 and 5 US-2
// pin 4 LED
//define for sensors and LED
#define trigPin1 5
#define echoPin1 6
#define trigPin2 7
#define echoPin2 8
#define led 4
#define SD_ChipSelectPin 10
TMRpcm tmrpcm;
long duration, distance, RightSensor,LeftSensor;
void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(led, OUTPUT);
{
tmrpcm.speakerPin=9;
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD fail");
return;
}
}
}
void loop()
{
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
Serial.print(LeftSensor);
Serial.print(" cm");
Serial.print(" - ");
Serial.print(RightSensor);
Serial.println(" cm");
delay(500);
}
{
void SonarSensor(int trigPin,int echoPin)
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 20)
{
tmrpcm.setVolume(6);
tmrpcm.play("test.wav");
digitalWrite(led,HIGH);
delay(10000)
tmrpcm.stop("test.wav");
}
else {
digitalWrite(led,LOW);
}
}
Please post the full text of the errors that you get