What I did was that I wrote a piece of code that will-in-fact move a servo motor when a clap is detected using a sound impact sensor (fc-04). The code for this is seen here and in fact does work:
#include "Arduino.h"
#include <Servo.h>
Servo myservo;//create servo object to control a servo
enum {
SERIAL_BAUD = 9600,
SENSOR_DIGITAL_PIN = 2,
SOUND_DELAY = 1500, /* Delay to avoid duplicate detection */
};
void setup() {
Serial.begin(SERIAL_BAUD);
pinMode(SERIAL_BAUD, INPUT);
myservo.attach(9);//attachs the servo on pin 9 to servo object
myservo.write(0);//back to 0 degrees
delay(1000);//wait for a second
}
void loop() {
if (digitalRead(SENSOR_DIGITAL_PIN) == LOW) {
static int count;
Serial.print("Sound detected: ");
Serial.println(++count);
myservo.write(170);//goes to 15 degrees
delay(3000);//wait for 3 seconds
myservo.write(10);//back to 0 degrees
delay(1000);//wait for a second
// Wait a short bit to avoid multiple detection of the same sound.
delay(SOUND_DELAY);
}
}
On a seperate occasion I paired two arduinos together using two hc-05 bluetooth modeules making one a slave and another a master, the initial code I used for both of them are listed here:
// == SLAVE CODE ==
#include <Servo.h>
#define button 8
Servo myServo;
int state = 20;
int buttonState = 0;
void setup() {
pinMode(button, INPUT);
myServo.attach(9);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
// Controlling the servo motor
myServo.write(state);
delay(10);
// Reading the button
buttonState = digitalRead(button);
if (buttonState == HIGH) {
Serial.write('1'); // Sends '1' to the master to turn on LED
}
else {
Serial.write('0');
}
}
and
// == MASTER CODE ==
#define ledPin 9
int state = 0;
int potValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
// Controlling the LED
if (state == '1') {
digitalWrite(ledPin, HIGH); // LED ON
state = 0;
}
else if (state == '0') {
digitalWrite(ledPin, LOW); // LED ON
state = 0;
}
// Reading the potentiometer
potValue = analogRead(A0);
int potValueMapped = map(potValue, 0, 1023, 0, 255);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);
}
I attempted to use the master and slave code with the one with the servo and fc-04 so that the sound sensor will be on the master and the servo on the slave so that when ever I clap it will move the servo on the other arduino.
The new master and slave code that I used are :
//New slave
#include "Arduino.h"
#include <Servo.h>
int state = 0;
Servo myservo;
void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
myservo.attach(9);//attachs the servo on pin 9 to servo object
myservo.write(0);//back to 0 degrees
delay(1000);//wait for a second
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
// Controlling the servo
if (state == '1') {
myservo.write(170);//goes to 15 degrees
delay(3000);//wait for 3 seconds
myservo.write(10);//back to 0 degrees
delay(1000);//wait for a second
state = 0;
}
else if (state == '0') {
state = 0;
}
}
//new Master
#include "Arduino.h"
#include <SoftwareSerial.h>
int state = 0;
SoftwareSerial BTserial(1, 0); // RX | TX
void setup() {
Serial.begin(9600);
pinMode(9600, INPUT);
BTserial.begin(38400);
}
void loop() {
if (digitalRead(2) == LOW) {
static int count;
BTserial.write('1');
delay(1500);
}
else {
BTserial.write('0');
}
}
However the slave and master code for the servo and fc-04 is not working at all despite knowing that all the conponents work individually. What I am aware of is that the fc-04 uses serial communication to transfer data and so does the hc-05 bluetooth modules. This causes an issue because I believe from experience is that you can not have two serial communications being transmitted from the arduino at the same time which I need. I have been at this for days and It is driving me nuts. If I could have some help at all I will be much appreciated.