Arduino code for password-activated output through bluetooth - bluetooth

I have this pretty simple task to light up an LED for 3 seconds when I send the right password from my Android phone to the Arduino via Bluetooth. Below's the code I am using:
Setting up variables:
int output = 9; //Pin 9 on arduino (LED)
char final[4];
char correct[4] = {'Q','W','E','R'}; //Password is QWER
int pass_true;
void setup() {
pinMode(output, OUTPUT);
Serial.begin(9600);
}
Here's the code in question:
void loop() {
while(Serial.available()){
for(int i=0; i<4; i++){
final[i] = Serial.read();
}
for(int i=0; i<4; i++){
if(final[i]==correct[i]){
pass_true = 1;
}
else{
pass_true = 0;
break;
}
}
}
if(pass_true==1){
digitalWrite(output, HIGH);
Serial.println("ON");
delay(3000);
pass_true = 0;
}
else{
digitalWrite(output, LOW);
}
}
As a newbie I can't see what's wrong with this code. I don't get any response from the arduino when sending the correct password. The LED doesn't turn on, the device doesn't return any Serial.println() string.
I see to it that the electrical part is all good so I'm pretty sure the problem is with the code. I am using Bluetooth spp pro Android app.

Related

Arduino bitRead() outputing binary number using LED

I am now trying to have my Arduino Uno to output a binary number sent from my mobile phone through bluetooth. The mobile phone would be sending an integer to the Arduino. Hopefully the Arduino converts the integer into binary and turning on corresponding LED. 4 LEDs are used to represent the binary number. However, the LED just flashes once or all the LED turn on when I input a number. Here is my code:
int li1;
const byte numPins = 4;
int pins[] = {10,11,12,13};
void setup () {
Serial.begin(19200);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop() {
while(!Serial.available());
li1 = Serial.read();
for (byte i=0; i<numPins; i++) {
byte temp = bitRead(li1, i);
digitalWrite(pins[i],temp);
}
}
li1 is the variable I get from the mobile phone.
Thank you for helping.
Probably not the answer, but do not have enough reputation to leave a comment.
Try setting a sleep after you write the bytes in case its going away to quick.
int li1;
const byte numPins = 4;
int pins[] = {10,11,12,13};
void setup () {
Serial.begin(19200);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop() {
while(!Serial.available());
li1 = Serial.read();
for (byte i=0; i<numPins; i++) {
byte temp = bitRead(li1, i);
digitalWrite(pins[i],temp);
delay(4000);
}
}

Sound sensor's embedded green light blinking with noise but external LED not

I bought a sound sensor ( 3 pins VCC GND OUT).
I connected all the pins correctly (I think)
VCC to 5V
GND to GND
OUT to A0
But when I upload the code into the arduino uno then the internal green light of sound sensor is blinking with the sound like music. But any LED at breadboard doesn't.
the value of the function sensorValue = analogRead(A0); return almost same value while making noise. After calibrating the sensor the value is also almost same.
Here my Code
#define out A0
int th = 320;
int sensorValue = 0;
int pins[3] = {10,11,12};
void setup(){
int i;
for( i = 0; i<3; i++ ){
pinMode(pins[i], OUTPUT);
}
//pinMode(out,INPUT);
Serial.begin(9600);
}
void loop(){
// sensorValue = analogRead(OUT);
sensorValue = analogRead(out);
int chk = digitalRead(out);
Serial.print("sensorValue: ");
Serial.println(sensorValue);
Serial.print("digitalRead: ");
Serial.println(chk);
int i=0;
if( sensorValue > th){
for( i = 0; i<3; i++){
digitalWrite(pins[i],HIGH);
}
}else{
for( i = 0; i<3; i++){
digitalWrite(pins[i],LOW);
}
}
}
What's wrong with it?

Arduino Leonardo keyboard cmd

I want access cmd in Windows 7 when a button on the board is pressed using an Arduino as a keyboard:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
char ctrlKey = KEY_LEFT_GUI;
void setup() {
// Initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// Initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// Read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
delay(1000);
Keyboard.press(ctrlKey);
Keyboard.press('r'); // This call runs
Keyboard.println("cmd"); // But here it won't write "cmd"
Keyboard.press('10');
Keyboard.releaseAll();
delay(1000);
}
else
{
digitalWrite(ledPin, LOW);
}
}
I also tried with the methods write(), print(), and press() for each letter individually, but it fails. Mostly it minimize all programs or invite all programs that are pinned in windows toolbar.
What is the problem?
Your code presses SuperR, SuperC, SuperM, SuperD, and then Super along with some null key. You need to release Super before attempting to fill the textbox, and you need to press Enter instead of that strange key once you've filled it.

Receiving multiple chars at once with Software Serial

I have a Arduino Uno R3 and a Bluetooth Mate.
When linking the Mate to the Arduino Hardware Serial (pin 0,1) I can send multiple characters at once from my connected device but when I try to make the same thing with Software Serial (using pin 4,2 for example) I only get the first character and the rest of the chars are messed up.
My code:
#include <SoftwareSerial.h>  
int bluetoothTx = 4;  
int bluetoothRx = 2;  
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
 Serial.begin(115200);  
 bluetooth.begin(115200);  
}
void loop()
{
 if(bluetooth.available())
 {
   Serial.print((char)bluetooth.read());  
 }
}
For example if I send this chars: abcd from my android device I get this in the serial monitor: a±,ö
This code that uses Hardware Serial (I link my bluetooth to pins 0 and 1) works just fine:
void setup()
{
 Serial.begin(115200);  
}
void loop()
{
 if(Serial.available())
 {
   Serial.print((char)Serial.read());  
 }
}
I even tried changing the baud rate but it didn't helped
If I send the chars one by one it works fine but I would like to be able to send them as a string.
You could try to bufferize the string before printing it.
Look at the following answer: Convert serial.read() into a useable string using Arduino?
As pointed out by #hyperflexed in the comments this is a baudrate related issue.
I had to take the baudrate as low as 9600 to make it work.
This is the code that worked:
#include "SoftwareSerial.h";
int bluetoothTx = 4;
int bluetoothRx = 2;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600);
delay(500);
bluetooth.begin(115200);
delay(500);
bluetooth.print("$$$");
delay(500);
bluetooth.println("U,9600,N");
delay(500);
bluetooth.begin(9600);
}
void loop()
{
if(bluetooth.available()) {
char toSend = (char)bluetooth.read();
Serial.print(toSend);
}
if(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.print(toSend);
}
}
For changing the baudrate I had to put some big delays to make sure the commands are executed otherwise it won't work.

How to convert the incoming characters coming from gsm module into a string?

Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.
#include <SoftwareSerial.h>
#include <String.h>
char inchar[255];
SoftwareSerial cell(2,3);
int led1 = 22;
#define powerOn 4
int i;
//char comparestring[160];
char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command ended with null terminator
void setup()
{
// ilagay sa loob ng setup
digitalWrite(powerOn, HIGH);
delay(1500);
digitalWrite(powerOn, LOW);
delay(5000);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.begin(9600);
cell.b-egin(9600);
delay(30000);
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("GSM SHIELD IS NOW OK AND READY");
}
void loop()
{
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
{
digitalWrite(led1, HIGH);
cell.write("AT+CMGS=\"");
cell.write("09267955775");
cell.write("\"\r");
delay(1000);
cell.write("\nTerminal Monitoring System");
delay(1000);
cell.write(0x1A); // End the SMS with a control-z
}
else
{
Serial.println("\nInvalid Keyword! Type ?");
dig-italWrite(led1, LOW);
}
}
}
these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
inchar is a string so try using strcmp(inchar,command) for comparing the two.

Resources