• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Voice controlled relay app

Q&A Forum › Voice controlled relay app
0 Vote Up Vote Down
Luix Gaztañaga asked 1 year ago

Hi, 
I am working on the Voice controlled app of the book titled Arduino step by step projects. The code is
 
int relay = 11; // pin Digital 11
int state; // saves the state
int flag=0; // makes sure that the serial only prints once
the state
void setup() {
// sets the Relay as output:
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is ‘0’ the relay will turn OFF
if (state == ‘0’) {
digitalWrite(relay, HIGH);
if(flag == 0){
Serial.println(“Relay Off!”);
flag=1;
}
}
// if the state is ‘1’ the relay will turn ON
else if (state == ‘1’) {
digitalWrite(relay, LOW);
if(flag == 0){
Serial.println(“Relay On!”);
flag=1;
}
}
//Uncomment For debugging purpose
//Serial.println(state);
}
 
 As far as I understand, the smarthphone sends via bluetooth “48” and “49” to say “off” and “on” but there is no clue about that in the code above.Only checks state whether it is 1 or 0 and this doesn’t make sense for me. I appreciate clarification.
Thanks .
Luix

Question Tags: Arduino step by step projects
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
The app sends ASCII values, that then the Arduino converts automatically to decimals.
 
In ASCII, 48 refers to 0, and 49 to 1.
https://theasciicode.com.ar/ascii-printable-characters/number-one-ascii-code-49.html
 
 
Regards,
Sara

0 Vote Up Vote Down
Luix Gaztañaga answered 1 year ago

Hi,
I understand it now. The project works fine. Thank you.
Luix

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

I should had that to the eBook.
Thanks for pointing that out.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.