I am trying to send the number "25" from arduino to android application created by using MIT AppInventor2. At the same time, I want to make "on" or "off" a LED by clicking "lock" and "unlock" buttons on the android app. There is no problem making LED status "on" and "off" but I can not read the 25 on the application. I only read 2 or 5. I read 25 very very rarely.
What is the problem about my arduino code or AppInventor blocks?
Arduino code:
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
Serial.println(25);// Here I want to send 25
delay(20);
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
// Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
Edit: Time interval for AppInventor clock is 1000.
Related
I just bought a Bluetooth HM-10 module with CC2541 chip. I am powering it with Arduino Uno. I scanned for bluetooth devices with my phone (samsung j3, 2016) and found the module named as BT05. I paired the devices but I could not connect the bluetooth module with any app. I tried to connect it with AMR Voice/BT Voice Control app and LED controller.
The code I used for controlling LED through app came from here: create.arduino.cc/projecthub/user206876468/arduino-bluetooth-basic-tutorial-d8b737
I put the code here aswell:
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is equal to
0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
These are the errors I get:
Maybe you can try if it's work and change the code by urself
#include <SoftwareSerial.h>
SoftwareSerial BT(2, 3); //RX, TX
char val;
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.println("BT is ready!");
}
void loop() {
if (BT.available()>0) {
val = BT.read();
Serial.print(val);
}
}
So I have a Bluetooth module that I've created some code for, so that when i push button 1 it turns on the led, when i push 2 it turns off the led, and 3 is supposed to make the led continuously blink. Everything works perfectly fine, except that when i push 3 the led blinks once then stops. What can i do to the code to make it blink continuously until i either hit button 1 or 2 again?
char LED = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // Opens Serial port
}
void loop() {
if (Serial.available()> 0){
LED = Serial.read();
Serial.print(LED);
if (LED == '1')
digitalWrite(13, HIGH);
if (LED == '0')
digitalWrite(13, LOW);
if (LED == '3') {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
}
You need to change main loop to:
void loop() {
if (Serial.available()> 0){
LED = Serial.read();
Serial.print(LED);
}
if (LED == '3') {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
else if (LED == '1')
digitalWrite(13, HIGH);
else if (LED == '0')
digitalWrite(13, LOW);
}
In this case LED == '3' will be work on every loop iteration, even if Serial is not contain any data
You put the blinking code into the if (Serial.available()> 0) block, which means that it's only run when you send a '3'. You would need to keep sending '3' to keep the LED blinking.
Sergey gave you a working solution, but that code unnecessarily keeps setting the LED on or off, and the code is unresponsive while the LED is blinking because of the use of delay.
To fix those problems you need to use a variable to keep a state (In Sergey's code that role took your existing LED variable) and use millis() instead of delay() for blinking, like in the BlinkWithoutDelay builtin Arduino IDE example.
char LED = 0;
enum {
NONE,
BLINKING,
} state = NONE;
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
Serial.begin(9600); // Opens Serial port
}
void loop() {
if (Serial.available() > 0){
LED = Serial.read();
Serial.print(LED);
switch (LED) {
case '1':
state = NONE;
digitalWrite(BUILTIN_LED, HIGH);
break;
case '2':
state = NONE;
digitalWrite(BUILTIN_LED, LOW);
break;
case '3':
state = BLINKING;
break;
}
}
switch (state) {
case NONE: break;
case BLINKING:
static unsigned long last_blink = 0;
if (millis() - last_blink > 1000)
{
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
last_blink = millis();
}
break;
}
}
i meet a issue. when i send a text message to my arduino board with a shield sim900 its show my message in number mode. who know what is the problem? here is my code. at the bottom i use serial monitor to see my message and it is read the message in number.
`
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
char incoming_char=0;
void setup()
{
Serial.begin(19200); // for serial monitor
SIM900.begin(19200); // for GSM shield
SIM900power(); // turn on shield
delay(25000); // give time to log on to network.
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(1000);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(1000);
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
void loop()
{
// Now we simply display any text that the GSM shield sends out on the serial monitor
if(SIM900.available() >0)
{
incoming_char=SIM900.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char); //Print the incoming character to the terminal.
if (incoming_char=='a')
{
digitalWrite(4,HIGH);
Serial.println(incoming_char);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
if (incoming_char=='b')
{
digitalWrite(4,LOW);
Serial.println(incoming_char);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
What I want to do is to control 2 leds in the arduino board with a processing program.
If I press any point on the upper half of the screen the led (Pin 13) will light on, and if I press a point on the lower half of the screen, it will turn on other led (Pin 12).
So, I programmed 2 buttons, with 2 leds(Pin 12 an 13) and no matter what button I press, It always turns on pin 13.
I made a separate experiment, with only 1 button, change only the pin 13 by 12. It does not work, always turns on pin 13.
ARDUINO CODE:
boolean estado;
boolean estado1;
byte a;
void setup()
{
Serial.begin(9600);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode (13, OUTPUT);
digitalWrite (13, LOW);
randomSeed(analogRead(0));
estado = false;
estado1 = false;
}
void loop()
{
delay(100);
Serial.write(random(40));
while(Serial.available() > 0)
{
a = Serial.read();
if (a == 0)
{
estado = !estado;
digitalWrite(12, estado);
}
if (a == 1)
{
estado1 = !estado1;
digitalWrite(13, estado1);
}
}
}
PROCESSING CODE:
void compruebaBoton()
{
if( mouseY < 640)
{
try
{
ons.write(0);
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
if( mouseY > 640)
{
try
{
ons.write(1);
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
}
the code I am implementing is correct for what I am trying to do?
After playing with the code for hours, I found the problem!!
The program is not loaded into the arduino if the bluetooth board is connected to the arduino board.
I am working with Aurdino and Amarino task. I am able to connect with Aurdino and I can send data to Aurdino from Android app. In the same way how I can access the data from Aurdino which I supposed to send to Android app(accessing data from aurdino means not the same data which I have sent from android app to aurdino). Aurdino can contain different data and I want to access that. Please help me
Regards,
Krishna
If you connect the Bluetooth RX to your Arduino TX (after uploading your sketch), you should be able to the data that is coming from Android with normal Serial operations. Here is an example sketch, that will turn LED on port 13 on if '1' is received otherwise it will be turned off
see this for a tutorial
You can also use a SoftSerial port
char val;
int ledpin = 13;
void setup() {
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if( Serial.available() )
{
val = Serial.read();
Serial.println(val);
}
if( val == '1' )
{
digitalWrite(ledpin, HIGH);
} else {
digitalWrite(ledpin, LOW);
}
delay(100);
}