How to send Data over Bluetooth Module HC-05 using Arduino? - bluetooth

/*
== MASTER CODE ==
*/
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ledPin 9
int state = 0;
int Vry = 0;
int Vrx = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop() {
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.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
//Vry = analogRead(A0);
/*
Vrx = analogRead(A1);
int VrxMapped = map(Vrx, 0, 1023, 0, 255);
//int Vry_mapped = map(Vrx, 0, 1023, 0, 255);
//int Vrx_mapped = map(Vry, 0, 1023, 0, 255);
Serial.print("Vrx");
Serial.println(VrxMapped);
//Serial.print("Vry");
//Serial.println(Vry);
*/
Vrx = analogRead(A1);
BTSerial.write(Vrx);
Serial.print("Vrx: ");
Serial.println(Vrx);
//BTSerial.write(Vry);
delay(2000);
}
and the slave code is as follows,
/*
== SLAVE CODE ==
*/
#include <SoftwareSerial.h>
#define button 8
SoftwareSerial BTSerial(5, 3); // RX | TX
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
int enB = 11;
int in3 = 7;
int in4 = 6;
int jx = A0;
int jy = A1;
int mx = 0; //right motor
int my = 0; //left motor
int state = 0;
int i = 0;
int buttonState = 0;
int ledPin = 13;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
BTSerial.begin(38400); // HC-05 default speed
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
//my = analogRead(BTSerial.read());
// mx = analogRead(BTSerial.read());
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
int state = BTSerial.read(); // Reads the data from the serial port
//Serial.print('y');
//Serial.println(my+100,DEC);
Serial.print('x');
Serial.println(state);
}
if (i == 2){
i=0;
}
/*int mapx = map(mx,0,1023,0,255);
int mapy = map(my,0,1023,0,255);
if (mapx>127)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA,mapx);
}
else
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA,127-mapx);
}
if (mapy>127)
{
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB,mapy);
}
else
{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB,127-mapy);
}
delay(1000);
*/
/*Serial.println(state);
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
if (state > 120){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
}
else{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
}
// Controlling the servo motor
// Reading the button
buttonState = digitalRead(button);
if (buttonState == HIGH) {
BTSerial.write('1'); // Sends '1' to the master to turn on LED
}
else {
BTSerial.write('0');
}
*/
delay(2000);
}
The problem here is following,
if I send 1 from master I get 130 at slave end, I have no idea how Serial communication works and how data can be received over bluetooth devices!

The serial of bluetooth in read function returns character.

Related

MSP432 GPIO Output Voltage Too Low

I'm an undergraduate student. We have an assignment to use Energia and the MSP432-P401R microcontroller to create an OR gate and AND gate IC tester.
You place the IC in the tester circuit and the tester will first determine if the IC is an AND or OR IC. Then, it will indicate which gates on the chip are functioning properly.
My issue is regrading the logical HIGH output voltage of the MSP432. My input signals, a and b, are passed to the IC gates. When my "a" signal is logical HIGH, its voltage is 2.8V. When my "b" signal is logical HIGH, its voltage is 0.7V. This 0.7V is too low for my ICs to register as a HIGH input. I am reading the voltage directly from the MSP432 pin.
I have tried using different GPIO pins and resetting the board. However, the "b" signal is still 0.7V.
My code for setting up "a" and "b" are similar, so I'm not sure why they are outputting different voltages.
My code:
//IC TEST
//Gate inputs pin assignment
int APin=11;
int BPin=8; //other pins tried: 12, 18, 5
//Gate output read pins
int output1Pin=38;
int output2Pin=37;
int output3Pin=36;
int output4Pin=35;
//LED pins
int gate1LEDPin=31;
int gate2LEDPin=32;
int gate3LEDPin=33;
int gate4LEDPin=34; //Gate functioning indicators
int orLEDPin = 40;
int andLEDPin= 39; //Logic of IC indicators
//Truth Table Arrays with inputs a and b
int aValue[]={0, 0, 1, 1};
int bValue[]={0, 1, 0, 1};
int orTrue[]={0, 1, 1, 1};
int andTrue[]={0, 0, 0, 1};
//Function Declaration
int workTest(int,int); //checks if gate is working
//by comparing gate output to
//expected truth result
int logicTest(); //Returns 0 for OR IC, 1 for AND IC
//Progrram variables
int i; //for loop counter
int gate1Result;
int gate2Result;
int gate3Result;
int gate4Result; //Stores number of times gate outputs
//correct value for each ab input
int logicRead; //0 is OR gate. 1 is AND gate
//--------------SETUP-----------------
void setup()
{
Serial.begin(9600);
pinMode(APin, OUTPUT);
pinMode(BPin, OUTPUT);
pinMode(output1Pin, INPUT);
pinMode(output2Pin, INPUT);
pinMode(output3Pin, INPUT);
pinMode(output4Pin, INPUT); //gate output reads
pinMode(orLEDPin, OUTPUT);
pinMode(andLEDPin, OUTPUT);
pinMode(gate1LEDPin, OUTPUT);
pinMode(gate2LEDPin, OUTPUT);
pinMode(gate3LEDPin, OUTPUT);
pinMode(gate4LEDPin, OUTPUT);
}
//END SETUP
//-----------------LOOP---------------------
void loop()
{
gate1Result = 0;
gate2Result = 0;
gate3Result = 0;
gate4Result = 0;
//test IC for its logic function
logicRead = logicTest(); //logicTest returns 0 for OR, 1 for AND
if(logicRead == 9) //logic of IC cannot be determined
{
Serial.println("Try again");
}
if(logicRead != 9)
{
for(i=0; i<4; i++)
{
digitalWrite(APin, aValue[i]); //Load ith value of aValue array
digitalWrite(BPin, bValue[i]); //Load ith value of bValue array
Serial.print("AB = ");
Serial.print(aValue[i]);
Serial.println(bValue[i]);
delay(4000); //Stabilize input signals
if(logicRead == 0) //OR Testing
{
Serial.print("OR Output should be ");
Serial.println(orTrue[i]);
Serial.print("Gate 1: ");
gate1Result = gate1Result + workTest(orTrue[i], output1Pin);
Serial.print("Gate 2: ");
gate2Result = gate2Result + workTest(orTrue[i], output2Pin);
Serial.print("Gate 3: ");
gate3Result = gate3Result + workTest(orTrue[i], output3Pin);
Serial.print("Gate 4: ");
gate4Result = gate4Result + workTest(orTrue[i], output4Pin);
}
if(logicRead == 1) //AND Testing
{
Serial.print("AND Output should be ");
Serial.println(andTrue[i]);
Serial.print("Gate 1: ");
gate1Result = gate1Result + workTest(andTrue[i], output1Pin);
Serial.print("Gate 2: ");
gate2Result = gate2Result + workTest(andTrue[i], output2Pin);
Serial.print("Gate 3: ");
gate3Result = gate3Result + workTest(andTrue[i], output3Pin);
Serial.print("Gate 4: ");
gate4Result = gate4Result + workTest(andTrue[i], output4Pin);
}
}
//Write gate 1 LED
if(gate1Result == 4)
{
digitalWrite(gate1LEDPin, HIGH);
Serial.println("Gate 1 works");
}
else
{
digitalWrite(gate1LEDPin, LOW);
Serial.println("Gate 1 FAIL");
}
//Write gate 2 LED
if(gate2Result == 4)
{
digitalWrite(gate2LEDPin, HIGH);
Serial.println("Gate 2 works");
}
else
{
digitalWrite(gate2LEDPin, LOW);
Serial.println("Gate 2 FAIL");
}
//Write gate 3 LED
if(gate3Result == 4)
{
digitalWrite(gate3LEDPin, HIGH);
Serial.println("Gate 3 works");
}
else
{
digitalWrite(gate3LEDPin, LOW);
Serial.println("Gate 3 FAIL");
}
//Write gate 4 LED
if(gate4Result == 4)
{
digitalWrite(gate4LEDPin, HIGH);
Serial.println("Gate 4 works");
}
else
{
digitalWrite(gate4LEDPin, LOW);
Serial.println("Gate 4 FAIL");
}
}
Serial.println();
Serial.println();
delay(10000); //Wait 10 sec before running code again
}//End void loop
//-----------Function Definitions-----------------------
//This function tests if all gates of the IC are working
//Returns 1 if gate is functioning
//Returns 0 if gate is not functioning
int workTest(int truth, int gateNum)
{
int gateRead = 0; //Updates for each gate output
int result = 0; //TotResult is 1 for all gates pass, 0 for any gate fail
gateRead= digitalRead(gateNum);
if(gateRead == truth)
{
result = 1;
}
else
{
result = 0;
}
Serial.println(gateRead);
return result; //1 for pass, 0 for gate fails
}
//This function tests the logic of the IC
//Returns 1 if IC is an AND gate
//Returns 0 if IC is an OR gate
//Allows for 1 broken gate
//If more than 1 gate broken, logic cannot
//be determined
int logicTest()
{
int aVal[]={0, 1};
int bVal[]={1, 0};
int outputCount = 0;
int logicGate = 0;
int c;
int gate1;
int gate2;
int gate3;
int gate4;
for(c=0; c<2; c++) //only testing ab = 01 and ab = 10
{
digitalWrite(APin, aVal[c]);
digitalWrite(BPin, bVal[c]);
delay(1000); //Stabilize input delay
gate1=digitalRead(output1Pin);
gate2=digitalRead(output2Pin);
gate3=digitalRead(output3Pin);
gate4=digitalRead(output4Pin);
outputCount = outputCount + gate1 + gate2 + gate3 + gate4;
}
if(outputCount < 3) //IC is likely AND gate w/ 1 broken gate
{
logicGate = 1;
Serial.println("IC is an AND gate");
digitalWrite(andLEDPin, HIGH);
digitalWrite(orLEDPin, LOW);
}
else if(outputCount > 5 && outputCount < 9) //IC is likely OR gate w/ 1 broken gate
{
logicGate = 0;
Serial.println("IC is an OR gate");
digitalWrite(andLEDPin, LOW);
digitalWrite(orLEDPin, HIGH);
}
else
{
logicGate = 9;
digitalWrite(andLEDPin, LOW);
digitalWrite(orLEDPin, LOW);
Serial.println("Logic of IC could not be determined");
Serial.println("IC could be broken. Make sure IC is connected properly.");
Serial.println();
}
return logicGate;
}
Here is the circuit setup:

arduino thread update volatile variable

I want to use multithread programming with Arduino.
I wrote some code ato update the variable tempsPose but it doesn't work (the led blinks always at the same speed).
How can I change the following code in order to update tempsPose variable in the function blinkled13 when this variable is mofified in the loop function?
#include <Thread.h>
Thread myThread = Thread();
int ledPin = 13;
volatile int tempsPose ;
void blinkLed13()
{
\\i would like the value of 'tempspose' to be updated
\\ when the value of the variable changes in the blinkLed13 function
while(1){
digitalWrite(ledPin, HIGH);
delay(tempsPose);
digitalWrite(ledPin, LOW);
delay(tempsPose);
}
}
void setup() {
tempsPose = 100;
pinMode(13,OUTPUT);
Serial.begin(9600);
myThread.onRun(blinkLed13);
if(myThread.shouldRun())
myThread.run();
}
void loop() {
for(int j=0; j<100; j++){
delay(200);
\\some code which change the value of 'tempsPose'
\\this code is pseudo code
tempsPose = tempsPose + 1000;
}
}
Examples are all "one-shot" (no infinite loops) and the code if (thread.shouldRun()) thread.run(); is inside of loop(). So I suppose it won't get into the loop() at all in your case.
For example more interactive code ('+' adds 100ms, '-' substracts 100ms):
#include <Thread.h>
Thread myThread = Thread();
int ledPin = 13;
volatile int tempsPose;
void blinkLed13() {
// i would like the value of 'tempspose' to be updated
// when the value of the variable changes in the blinkLed13 function
static bool state = 0;
state = !state;
digitalWrite(ledPin, state);
Serial.println(state ? "On" : "Off");
}
void setup() {
tempsPose = 100;
pinMode(13,OUTPUT);
Serial.begin(9600);
myThread.onRun(blinkLed13);
myThread.setInterval(tempsPose);
}
void loop() {
if(myThread.shouldRun()) myThread.run();
if (Serial.available()) {
uint8_t ch = Serial.read(); // read character from serial
if (ch == '+' && tempsPose < 10000) { // no more than 10s
tempsPose += 100;
myThread.setInterval(tempsPose);
} else if (ch == '-' && tempsPose > 100) { // less than 100ms is not allowed here
tempsPose -= 100;
myThread.setInterval(tempsPose);
}
Serial.println(tempsPose);
}
}

Need expertice on code for Arduino based security system

Here is my code so far the system contains an SD card reader, two buzzers, two LEDs, a PIR motion sensor, and a IR receiver.
error message I get are as follows:
1) Final_Build:100: error: redefinition of 'int val'
2) Final_Build:5: error: 'int val' previously defined here
3) Final_Build:101: error: expected unqualified-id before 'if'
4) redefinition of 'int val'
//PIR sensor
int ledPin = 7; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
//IR_Sensor
int dtect=3;
int sense=A0;
int buzzpin=9;
int lightPin=6;
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
//PIR sensor
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
//IR sensor
pinMode(dtect,OUTPUT);
pinMode(lightPin,OUTPUT);
pinMode(sense,INPUT);
pinMode(buzzpin,OUTPUT);
digitalWrite(dtect,HIGH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop(){
//IR sensor
int val=analogRead(sense);
Serial.println(val);
if(val>=800)
{
digitalWrite(lightPin, HIGH);
buzz(50);
}
else {
digitalWrite(lightPin, LOW);
}
}
void buzz(unsigned char time)
{
analogWrite(buzzpin,170);
delay(time);
analogWrite(buzzpin,0);
delay(time);
}
//PIR sensor
int val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(100);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
This first val definition isn't necessary.
int val = 0; // variable for reading the pin status
You are redifining val at the begining of loop() .
UPDATE:
I think that this is what you want. Your problem is that added buzz function was defined inside loopfunction and you lost some parentesis{}. I just put it out and joined loopcode.
//PIR sensor
int ledPin = 7; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
//IR_Sensor
int dtect=3;
int sense=A0;
int buzzpin=9;
int lightPin=6;
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
//PIR sensor
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
//IR sensor
pinMode(dtect,OUTPUT);
pinMode(lightPin,OUTPUT);
pinMode(sense,INPUT);
pinMode(buzzpin,OUTPUT);
digitalWrite(dtect,HIGH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop(){
//IR sensor
int val=analogRead(sense);
Serial.println(val);
if(val>=800)
{
digitalWrite(lightPin, HIGH);
buzz(50);
}
else {
digitalWrite(lightPin, LOW);
}
//PIR sensor
int val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(100);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void buzz(unsigned char time)
{
analogWrite(buzzpin,170);
delay(time);
analogWrite(buzzpin,0);
delay(time);
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}

SoftwareSerial issues. Only when on power jack

My Code:
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2,3);
// Output
int redPin = 6; // Red LED,
int grnPin = 11; // Green LED,
int bluPin = 5; // Blue LED,
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 100, 100, 100 };
int red[3] = { 100, 0, 0 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.
// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int wait = 10; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int r = 0;
int g = 0;
int b = 0;
char mode = '\0';
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
// Set up the LED outputs
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
Serial.begin(9600);
delay(1000);
bluetooth.begin(115200);
delay(100);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
delay(100);
analogWrite(redPin, 0);
analogWrite(grnPin, 0);
analogWrite(bluPin, 0);
Serial.println("Bluetooth initiated.");
}
void printHEX() {
Serial.print(r, HEX);
Serial.print(g, HEX);
Serial.println(b, HEX);
bluetooth.print(r, HEX);
bluetooth.print(g, HEX);
bluetooth.println(b, HEX);
}
// Main program: list the order of crossfades
void loop()
{
//Read from bluetooth and write to usb serial
while(bluetooth.available())
{
if(mode == '\0') {
mode = (char)bluetooth.read();
}
if(mode == 'c'){
int r1 = bluetooth.parseInt();
int g1 = bluetooth.parseInt();
int b1 = bluetooth.parseInt();
if (bluetooth.read() == '\n') {
if(r1 != r || g1 != g || b1 != b) {
r = r1;
g = g1;
b = b1;
analogWrite(redPin, r);
analogWrite(grnPin, g);
analogWrite(bluPin, b);
printHEX();
mode = '\0';
} else {
printHEX();
mode = '\0';
}
}
} else if(mode == 'p') {
if (bluetooth.read() == '\n') {
printHEX();
mode = '\0';
}
}
}
//Read from usb serial to bluetooth
if(Serial.available())
{
char toSend = (char)Serial.read();
bluetooth.print(toSend);
}
}
If I run this code, everything works great. That is until I plug it into the power source and nothing else.
If I plug it into the power source, the program doesn't start (no bluetooth response). If I plug it into usb and power or usb only, the program works. If I unplug usb after plugging usb and power source the program still works! I have tried debugging as much as I can, but I don't know where the error is. The power supply is rated at 12V 2 Amps to light up the LED strips.
Update: I found out that if I press the reset button after power on everything starts to work. Is there a way to automatically reset arduino on startup???
I think you are using arduino leonardo.
Try this at very beginning of setup:
while(!Serial){}
while(!bluetooth){}
The arduino leonardo prepare the serial port after some while and may cause some problems.

2 PIR motion sensors +Arduino

My project is to allow automatic lightening by detecting motion using PIR Sensors.
THis is what I want to do :
when the first motion sensor "inputpin" is HIGH which means a motion1 is detected , ledPin1 is set to HIGH.... then I check the signal from the other PIR sensor "inputpin2" if it is HIGH ledPin3 should be HIGH , If it is LOW ledpin2 should be HIGH.
I wrote this code , but what it actually do is after a motion is detected from the first sensor"inputPin" , ledPin3 is set to high as if the second sensor is always HIGH !
Can any one help me with this problem.
Thanks
` ``
int ledPin1 = 13; // choose the pin for the LED
int ledPin2 = 12;
int ledPin3 = 11;
int inputPin = 2; // choose the input pin (for PIR sensor)
int inputPin2 = 1;
int pirState1 = LOW; // we start, assuming no motion detected
int pirState2 = LOW;
int val = 0; // variable for reading the pin status
int val2 = 0;
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
pinMode(ledPin1, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(ledPin3, OUTPUT);
pinMode(inputPin, INPUT); // declare sensor 1 as input
pinMode(inputPin2, INPUT); // declare sensor 2 as input
// pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin1, HIGH); // turn LED ON
delay (1500);
if (pirState1 == LOW) {
// we have just turned on
Serial.println("Motion1 detected!");
// We only want to print on the output change, not state
pirState1 = HIGH;
}
delay (1500);
// check sensor 2 after delay
val2 = digitalRead(inputPin2);
if (val2 == HIGH){
digitalWrite(ledPin2, LOW);
delay(1500);
digitalWrite(ledPin3,HIGH);
//playTone(300, 160);
delay(1500);
if (pirState2 == LOW) {
// we have just turned on
Serial.println("Motion1 from sensor 2 detected!");
// We only want to print on the output change, not state
pirState2 = HIGH;
}
}
if(val2 == LOW){
digitalWrite(ledPin2, HIGH);
//playTone(300, 160);
delay(1500);
digitalWrite(ledPin3,LOW);
delay(1500);
}
} else {
digitalWrite(ledPin1, LOW); // turn LED OFF
delay (1500);
digitalWrite(ledPin2, LOW); // may be already
//playTone(0, 0);
delay(1500);
digitalWrite(ledPin3, LOW); // turn LED OFF
delay (1500);
if (pirState1 == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState1 = LOW;
}
if (pirState2 == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState2 = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
`
Change inputPin2 from 1 to 3 for example. Just don't use the pins 0 or 1 (those assigned for Tx and Rx), hope this works.

Resources