Problem with blinking a led with bluetooth in arduino - arduino

i am trying to make a program that turns on ,off and blinks an led with the help from bluetooth
On and of were pretty easy to replicate,but i can't make the blink to work.There are to options either blinks once,either if i ad a while it never stops from looping.i tried with both if and case.Can somebody help me.I am using an esp32.
The code with if:
#include "BluetoothSerial.h"
#include <Arduino.h>
#include <analogWrite.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='a';
const char turnOFF ='b';
const char turnBLINK= 'c';
//const char turnFADE='d';
const int LEDpin = 12;
//int brightStep = 1;
//int brightness = 0;
void setup() {
Serial.begin(115200);
SerialBT.begin("Mono"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: a");//print on serial monitor
Serial.println("To turn OFF send: b"); //print on serial monitor
pinMode(LEDpin, OUTPUT);
// analogWriteResolution(LEDpin, 12);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
// while(SerialBT.available()){
// receivedChar =(char)SerialBT.read();
// }
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("LED ON:");// write on BT app
Serial.println("LED ON:");//write on serial monitor
digitalWrite(LEDpin, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("LED OFF:");// write on BT app
Serial.println("LED OFF:");//write on serial monitor
digitalWrite(LEDpin, LOW);// turn the LED off
}
if(receivedChar == turnBLINK)
{
SerialBT.println("LED blink:");// write on BT app
Serial.println("LED blink:");//write on serial monitor
while (receivedChar == turnBLINK){
//receivedChar =(char)SerialBT.read();
//if(receivedChar != turnBLINK){
// break;
// } else {
digitalWrite(LEDpin, HIGH);// turn the LED off
delay(1000);
digitalWrite(LEDpin,LOW);
delay(1000);
if(receivedChar != turnBLINK){
break; }
}
}
}
delay(20);
}
and with case:
#include "BluetoothSerial.h"
#include <Arduino.h>
#include <analogWrite.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
char data;
int option;
int blink=0;
const int LEDpin = 12;
void setup() {
Serial.begin(115200);
SerialBT.begin("Mono"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: 1");//print on serial monitor
Serial.println("To turn OFF send: 0"); //print on serial monitor
pinMode(12, OUTPUT);
}
void loop(){
receivedChar =(char)SerialBT.read();
// if (Serial.available()) {
// SerialBT.write(Serial.read());
// }
if (SerialBT.available()) {
// while(SerialBT.available()){
// receivedChar =(char)SerialBT.read();
// }
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
data=receivedChar;
if(data == '0')
{
option = 0;
blink=0;
}else if(data == '1')
{
option = 1;
blink=0;
}else if(data == '2')
{
option = 2;
blink=1;
}
switch (option)
{
case 0: // LED OFF
digitalWrite(LEDpin, LOW);
break;
case 1: //LED ON
digitalWrite(LEDpin, HIGH);
break;
case 2:
while(blink=1){// LED BLINK
digitalWrite(LEDpin , HIGH);
delay(200);
digitalWrite(LEDpin, LOW);
delay(200);
}
break;
}
}
}

First of all, you should clean your code to a minimum reproducible example. Remove all unnecessary comments and pieces of code that do not represent the main problem you are facing.
After a quick skim over your code, I immediately noticed this:
while(blink=1){// LED BLINK
digitalWrite(LEDpin , HIGH);
delay(200);
digitalWrite(LEDpin, LOW);
delay(200);
}
where it should be while(blink==1){ } --> very common mistake. This should be a comparison, NOT an assignment.
Now, you mention that it never stops running. Even after correcting the error I just pointed at, what part inside of your while loop breaks the logic of blink from being equal to 1? Otherwise, the while-loop will never stop
Finally, do not read the serial data inside the main loop(). Use SerialEvent, rather.
Again, it is quite tricky to follow the flow of your code. I suggest you divide your code into functions in order to make it more readable.

Related

Why does VirtualWire seem to receive transmission twice?

I have a simple setup with two Arduinos and a 433MHz transmitter and a receiver module.
The transmitter is set to transmit a string on button press, that side works correctly.
On the receiver end I have a simple program that's just writing whatever it's receiving to serial:
#include "VirtualWire.h"
int rx_pin = 2;
void setup(){
Serial.begin(9600);
Serial.println("serial ready");
vw_set_rx_pin(rx_pin);
vw_setup(2000);
vw_rx_start();
Serial.println("receiver ready");
}
void loop(){
uint8_t msg[VW_MAX_MESSAGE_LEN];
uint8_t msglen = VW_MAX_MESSAGE_LEN;
vw_wait_rx();
if(vw_get_message(msg, &msglen)){
Serial.print("Got: ");
for (int i = 0; i < msglen; i++)
{
Serial.print(char(msg[i]));
}
Serial.println();
}
}
When I then monitor serial, the receiving Arduino seems to receive the message twice each time it's sent. I used an oscilloscope to verify (to the best of my knowledge) that the transmitter is only sending the message once, I also tried wiring the two Arduinos together to make sure the issue is not with the RF modules, I got the same results.
This makes me think there is an issue with my code or with VirtualWire itself.
I'm thinking that I should somehow check if I've received the same message already or I should clear VirtualWire's buffer, how would I do either of those?
EDIT:
Here is the transmitter code:
#include "VirtualWire.h"
int tx_pin =2;
int interruptPin = 3;
volatile bool transmitBool = false;
void setup(){
vw_set_tx_pin(tx_pin);
vw_setup(2000);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), transmit, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
transmitBool = false;
}
void loop(){
if(transmitBool) {
transmitBool = false;
digitalWrite(13, HIGH);
vw_send((uint8_t *)"abc", 4);
vw_wait_tx();
delay(500);
digitalWrite(13, LOW);
}
}
void transmit() {
transmitBool = true;
}

Arduino control with serial input more then 1 character

I have the script below and it works.
But I want to control it with a word like on or off instead of one character.
I tried and searched a lot but without success.
/*
Simple LED sketch
*/
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(115200); // baud rate
}
void loop()
{
if (Serial.available()) {
int ser = Serial.read(); //read serial as ascii integer
if (ser == 'a') { //is this serial byte the ASCII equivalent of 0 through 9?
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (ser == 'u') {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Use Serial.readStringUntil(terminator) to read a string from the serial.
The sent string needs to be terminated with a newline character.
Chose Newline in the Arduino IDE's Serial Monitor.
String cmd = "";
void loop()
{
if (Serial.available()) {
cmd = Serial.readStringUntil('\n');
if (cmd == "on") {
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (cmd == "off") {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Your problem is that you are using a char you need to declare a string so you can compare that string to the input, the guy above gave a ok/very slacky/too complex for you answer (sorry no offense I am just trying to show contrast between a complex and a easy answer not trying to offend you) and partially is because he did not explain what he is doing and because he is doing unnecessary/useless work. There is a function in c++ called Serial.readString(), much easier then the stuff up there. Assuming your level of programming (no offense) from your question here is a quick review on data types:
String = ""
int = integer number {1,2,3,4,5,6,...}
char = '' <- Notice the difference from String = ""
float = floating point number {1.2,4.5,...}
(These are not all of them, there is more like byte and so on but just make sure you know how to use the above first)
/*
Simple LED sketch
*/
int led = 7; // Pin 13
String ser; //Declare the string that is going to store what your are going to
//write in the serial
void setup(){
// Start up serial connection
//It's good convention to start the serial before pinMode or any other thing
//in setup
Serial.begin(9600); //I don't know why you need such an high baud rate in
//your thing, 9600 for what you have to do is more than fine, this is just at
//what speed the serial is read (in a very general explanation)
pinMode(led, OUTPUT); // Set pin 13 as digital out
//make sure you put in here wheter you want to start the led "on" or "off"
//Eg you want it to start on, then digitalWrite(led, HIGH);
}
void loop(){
ser = Serial.readString();
if(Serial.available() == 0) { //You can also use a while loop if you want to
//This is telling the arduino: if there is something in the serial, then do...
if(ser == "on"){
Serial.println("on");
digitalWrite(led, HIGH);
}else if(ser == "off"){
Serial.println("off");
digitalWrite(led, LOW);
}
}
}
Hope it helped!
Also notice how the above code with
if(Serial.available())
This is a quite WEIRD and SHADY statement and might not work. That's because you are not really telling the int value into the function Serial.available
As the arduino guidelines specify:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
viewable here

RFid click board with CR95HF microcontroller refuses to respond

I have an Arduino with a RFid click board from Mikro Electronika. The click board has a microcontroller CR95HF microcontroller. According to the datasheet when I send the ECHO command I should poll and then read the returned value 0x55. If I use the command IDN (0x01) and send a datalength of zero, I should get the ID of the controller back.
According to the serial monitor it only returns zero (is there something wrong with the variable?).
According to the logic analyzer it returns the exact same thing as what was send
(when I send 3, it returns 3, when i send 55, it returns 55).
It seems the microcontroller doesn't react anything I send it or it does and it's just broken?
So right now it looks like it is able to send something through the spi.transfer() function, but the retrieve seems to be a problem.
Here's my code:
#include <SPI.h>
// CR95HF: Write, poll, read, reset
#define CMD 0x00
#define POLL 0x03
#define READ 0x02
#define RESET 0x01
#define ECHO 0x55
const int _SSpin = 10; // CS
const int _IRQpin = 6; // INT_out : 2, INT_in : 6, tried both, barely to no difference
//const int _SI0pin = A3;
//const int _SI1pin = A0;
void WakeupCR95HF()
{
//Serial.println("Wake up!");
digitalWrite(_IRQpin, LOW);
delay(100);
digitalWrite(_IRQpin, HIGH);
delay(100);
}
bool EchoResponse()
{
byte len = 0;
// write to CR95HF
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x00);
// Serial.print("echo code: ");
// Serial.println(ECHO);
SPI.transfer(0x55);
//SPI.transfer(0x00);
digitalWrite(_SSpin, HIGH);
//delay(1000);
// poll to CR95HF, is it ready?
byte tmp = 0;
digitalWrite(_SSpin, LOW);
while (!tmp)
{
delay(100);
//Note: whatever I send, I get the same thing back. I send 3, then tmp is 3. I send 8, tmp is 8.
SPI.transfer(0x03);
tmp = SPI.transfer(0);
// Serial.print("Polling: ");
Serial.println(tmp, BIN);
tmp = (tmp & 0x08) >> 3;
delay(100);
}
digitalWrite(_SSpin, HIGH);
delay(20);
// ready to write
byte res = 0;
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x02);
res = SPI.transfer(0);
// Serial.print("reading: ");
// Serial.println(res);
delay(100);
digitalWrite(_SSpin, HIGH);
delay(100);
if (res == ECHO)
{
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
// I believe this is not possible on Arduino or not nessaccery, tried it anyway, no change
// pinMode(_SI0pin, OUTPUT);
// pinMode(_SI1pin, OUTPUT);
pinMode(_SSpin, OUTPUT);
pinMode(_IRQpin, OUTPUT);
// digitalWrite(_SI0pin, HIGH);
// digitalWrite(_SI1pin, LOW);
digitalWrite(_IRQpin, HIGH); // wakeup
delay(20);
digitalWrite(_SSpin, HIGH); // slave select, low = ready to write/read
delay(20);
SPI.begin();
// 2 MHz max clock speed, Most Significant Bit first, SPI_MODE unknown (probably 0, all were tried)
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
WakeupCR95HF();
while(!EchoResponse())
{
WakeupCR95HF();
}
// Serial.println("Connection established");
}
void loop() {
}

Serial.read() - XBee - not firing 'else if'

I am currently trying to do some communication test between a PC and Arduino Uno using an XBee in AT mode.
My test is to send characters from the computer to the XBee and process through conditional statements.
I don't believe this issue is with configuration of the XBees, for I am able to communicate successfully when I watch the Serial monitors.
Here is the code I am running on the Arduino:
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
if (xBee.available()> 0)
{
if (xBee.read() == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if (xBee.read() == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
I can turn on the LED when sending the character 'r' from the PC to the XBee. The intended result is received back as well. When I try to send the character 'o' from the PC the LED stays on, and I get the response of "NR".
This same result happens with different characters in the else if statement, sending character 'o' as the first character, changing to just if statements, and changing the initial condition to - while xBee.available().
How can I fix this problem?
You need to store the input value of xBee.read() and then use it in the if condition.
You can try this
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
char read_value = xBee.read();
if(xBee.available()> 0)
{
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if ( read_value == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
The problem is that you are taking the input with xBee.read() but not storing it.
Only your first if works ie,
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
The control is not even going in the else if hence condition for o is not tested.

How to read an SMS from Arduino and get LED to switch on or off

#include <SoftwareSerial.h>
char inchar; //Will hold the incoming character from the serial port.
SoftwareSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
int led1 = A2;
void setup()
{
// Prepare the digital output pins
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH);
//Initialize GSM module serial port for communication.
cell.begin(19200);
delay(30000); // Give time for GSM module to register on network, etc.
cell.println("AT+CMGF=1"); // Set SMS mode to text
delay(200);
cell.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If a character comes in from the cellular module...
if(cell.available() >0)
{
delay(10);
inchar=cell.read();
if (inchar=='a')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
delay(10);
}
cell.println("AT+CMGD=1,4"); // Delete all SMS
}
}
This is the code for receiving SMSes from the cellular network. I am using the Arduino Gboard with SIM900. There is no error in the code, but the LED on the board doesn't switch on or off in response to an SMS.
Why?
Here's a fully functional code for sending a command thru SMS using Arduino and GSM, it will also reply the state of the light.
#include <SoftwareSerial.h>
SoftwareSerial GPRS(10, 11);
String textMessage;
String lampState;
const int relay = 12; //If you're using a relay to switch, if not reverse all HIGH and LOW on the code
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // The current state of the light is ON
Serial.begin(9600);
GPRS.begin(9600);
delay(5000);
Serial.print("GPRS ready...\r\n");
GPRS.print("AT+CMGF=1\r\n");
delay(1000);
GPRS.print("AT+CNMI=2,2,0,0,0\r\n");
delay(1000);
}
void loop(){
if(GPRS.available()>0){
textMessage = GPRS.readString();
Serial.print(textMessage);
delay(10);
}
if(textMessage.indexOf("ON")>=0){ //If you sent "ON" the lights will turn on
// Turn on relay and save current state
digitalWrite(relay, HIGH);
lampState = "ON";
Serial.println("Lamp set to ON\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched ON.\r");
GPRS.write( 0x1a );
delay(1000);
}
if(textMessage.indexOf("OFF")>=0){
// Turn off relay and save current state
digitalWrite(relay, LOW);
lampState = "OFF";
Serial.println("Lamp set to OFF\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); /// RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched OFF.\r");
GPRS.write( 0x1a );
delay(1000);
}
if(textMessage.indexOf("STATUS")>=0){
String message = "Lamp is " + lampState;
GPRS.print("AT+CMGF=1");
delay(1000);
Serial.println("Lamp state resquest");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp is currently ");
GPRS.println(lampState ? "ON" : "OFF"); // This is to show if the light is currently switched on or off
GPRS.write( 0x1a );
delay(1000);
}
}
Change
AT+CNMI=3,3,0,0
to:
AT+CNMI=2,2,0,0,0
The simplest way is the best way.
// if You use SoftwareSerial lib, declare object for GSM
SoftwareSerial gsm(8,9); // TX, RX
void setup(){
// initialise UART and GSM communication between Arduino and modem
Serial.begin(115200);
gsm.begin(115200);
// wait 5-10sec. for modem whitch must connect to the network
delay(5000);
// configure modem - remember! modem didn't remeber Your's configuration!
gsm.print("at+cmgf=1\r"); // use full functionality (calls, sms, gprs) - see app note
gsm.print("at+clip=1\r"); // enable presentation number
gsm.print("at+cscs=\"GSM\"\r"); // configure sms as standard text messages
gsm.print("at+cnmi=1,2,0,0,0\r"); // show received sms and store in sim (probobly, I don't compre this settings with app note but it's working :)
}
void loop(){
String response = gsmAnswer();
if(response.indexOf("+CMT:") > 0 ) { // SMS arrived
// Now You can parse Your Message, if You wont controll only LED, just write
if(response.indexOf("LED ON") > 0) {
digitalWrite(LED_PIN, HIGH); // enable it
}else if(response.indexOf("LED OFF") > 0) {
digitalWrite(LED_PIN, LOW); // turn off
}
delay(1000);
}
}
String gsmAnswer(){
String answer;
while(!gsm.available());
while(gsm.available()){
delay(5);
if(Serial.available() > 0){
char s = (char)gsm.read();
answer += s;
}
}
return answer;
}
One think more, incomming sms has the following format:
+CMT: "+48xxxxxxxxx","","17/07/07,21:57:04+08"
Test of arrived messages
You should first know exactly what the response is before attempting to parse it.
Try something simple like the following code (note: untested!) to get a feeling of what you should look for:
void loop() {
if(cell.available() > 0) {
char ch = cell.read();
Serial.print(ch);
}
}
My guess is you'll see more than just a '0' or a '1' :)
void loop() {
while(cell.available() > 0) {inchar = cell.read(); readString+=c;delay(1);} ///can be a delay up to (10) so you can get a clear reading
Serial.print(readString); /// Declare a string " String readString; "
for (i=0; i<200; i++){ /// Serch for the txt you sent up to (200) times it depends how long your ""readString" is
if(readString.substring(i,i+4)=="RING"){ //// I am looking for the word RING sent from my phone
digitalWrite(13,HIGH);
break;
}
}
}
this will help you find the specific word ir your reading (text message)

Resources