My project is to control the LED by send '1' or '0' via serial monitor.
My task for this project is when '1' is send via serial monitor, the Led ON PIN 3 need to turn on and off every 2000ms. Then, when '0' is send via serial monitor, the LED need to be turn off until next '1' is send , so that the Led ON PIN 3 can be turn on and off every for 2000ms again. But it doesn't work for my code, can anyone tell me what is wrong in my code. Below is my code:
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(115200); //Sets the baud for serial data transmission
pinMode(3, OUTPUT); //Sets digital pin 3 as output pin
}
void loop()
{
if(Serial.available()>0 ) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data send via serial monitor & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
while(data == '1') //Do looping so that when '1' send via serial monitor, the LED can blink
{
digitalWrite(3, HIGH);
delay(2000);
digitalWrite(3, LOW);
delay(2000);
}
while(data == '0') // Checks whether value of data is equal to 0
digitalWrite(3, LOW); //If value is 0 then LED turns OFF
}
}
You have several obvious mistakes... The main one being that you have locked execution into the while loops. So the execution path gets to "while data==" and it will stay there while the data equals that value. That value though can't change as you only read the value of data at the beginning of the function loop(). The only way this could work is if loop were serviced by a timer function and shared between two threads.
Replace your whiles with ifs, and run it using a while (1){loop();}; you may find it goes then. TBH, I'd add the Delay 2000 at the while loop level and only query the serial port every 2000mS too. It's bad karma to hammer things in a flywheel loop.
Change the structure of your loop.
Let the loop execute every 2000 ms. If there is data availabe to read from serial, read and parse it to update the 1 / 0 state.
Dependent on state, either let the state of the LED toggle between on and off or leave it in off state.
Related
This is most probably a software question, not sure where to post this (Arduino sub? electronics sub?).
I have an Arduino Mega, that is connected to :
an RFID card read via SPI,
a wifi chip (esp8266-12f) via Serial1.
The wifi chip currently acts as a web Server as I intend to send commands to it via the internet. I also want that wifi chip to act as a Client, that is to say : when the RFID reads a card, I want the Arduino Mega to forward the RFID's card id to the wifi chip via Serial1 so that the wifi chip can send an http request to a dedicated server that would save it into sql database.
This is how I coded it on the wifi chip so far :
// esp as a Server
// [...]
// esp as a Client
timeout = millis() + (10 * 1000UL); // 10s
String input;
while (Serial.available() >= 0 && millis() < timeout) {
input += Serial.readStringUntil('\r');
int pos = input.indexOf("AT+TEST");
if (pos >= 0) {
Serial.println(F("AT+TEST"));
// collate, aka confirm to Arduino Mega we understood the command and we are processing it
// todo: process the command, aka parse the command, send an http request, etc
Serial.println(F("\r\nOK"));
} else {
Serial.println(F("\r\nNOPE"));
}
}
As you might have guessed, the chip is printing "AT+TEST" over and over again (until it times out, then loops infinite NOPE), as it is trying to process the command over and over, but I just want it to say "ok Arduino Mega, I understood you want me to execute AT+TEST"
What would be an elegant way to make it so that the wifi chip "ignores" the stuff it itself prints to it's own serial? It should only execute commands coming from the Arduino Mega.
This is something the factory default firmware does very well : when I send "AT", it echoes back "AT\r\n\r\nOK" only once.
I found this but I cannot understand how they implemented the idea (irrelevant code everywhere) : https://github.com/JiriBilek/ESP_ATMod/blob/d6ad63f71ef3189518ef4740f16a9d697c3e843a/ESP_ATMod/ESP_ATMod.ino#L581
Looking for guidance on a solid way to achieve this as there are lots of datas coming in and out to that serial of the wifi chip.
It looped because of the Line Feed. Not only that, it also needed a 1ms delay somehow.
Sorry for the poor explanation of my issue.
The following works as I expected :
// esp as a client
String input;
timeout = millis() + (5 * 1000UL); // 5s
while (Serial.available() && millis() < timeout) {
input = Serial.readStringUntil('\r'); // up to CR
delay(1); Serial.read(); // discard LF
// collate
Serial.println(input);
// AT+TEST
if (input.indexOf("AT+TEST") >= 0) {
Serial.println(F("\r\nOK"));
// todo : verbose for debug
}
// AT+GET=https://arduino.example.tld/api/arduino.php?token=123&key1=val1
else if (input.indexOf("AT+GET") >= 0) {
Serial.println(F("\r\nOK"));
// todo : regex + http get req
} else {
Serial.println(F("\r\nERROR"));
}
}
Device description (for context, skip it if you don't feel comfortable with electronic):
For a simple device, the communication is done in half-duplex UART (TX and RX are on the same wire), in the following way:
One pin (write-mode) indicate if the UART is sending or receiving (1: TX, 0:RX)
One pin write to the wire (TX).
One pin read from the wire (RX).
When the write-mode is in TX (writing), the RX pin is in high-impedance and TX in transmitting. While the write-mode is in RX (reading), the TXpin is in high-impedance and RXreceiving.
This is just for context, I do not expect electronic question/answers here.
WiringPI sample:
For this to happens, I have the following sample:
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
wiringPiSetup ();
auto fd = serialOpen ("/dev/ttyAMA0", 115200);
pinMode(0, OUTPUT);
for(size_t i=0; i<10; ++i)
{
digitalWrite(0, HIGH);
serialPutchar(fd, '\x55');
digitalWrite(0, LOW);
delay(1000);
}
serialClose(fd);
}
Using an oscilloscope, I can clearly see that the write-mode pin is reset before the UART end to send the data.
Obviously, I tried to add some "delay" or empty-loop to adjust, but this is not reliable for μs times (due to usual precision in timers on OS).
The question:
How to synchronize, so the write-mode pin is reset just after the UART byte is sent? (No more than approximately 150μs later).
I see 2 ways to implement this:
1. I can't test this right now, but it seems you could use the
void serialFlush (int fd) ;
According to the docs "This discards all data received, or waiting to be send down the given device" see at http://wiringpi.com/reference/serial-library/
(Edit: after re-reading that sentence, it's clear that it would indeed flush also the data to be written, so this option is out...)
Using tcdrain() (https://linux.die.net/man/3/tcdrain), where you pass the fd given back by serialOpen()
Set blocking on the file descriptor.
How to restore file descriptor operation to blocking mode can be found in many places. You may use code from this answer and do:
set_blocking_mode(fd, 1);
serialPutchar(fd, '\x55');
After that write() inside serialPutchar will be blocking. Note that fd is opened with O_NONBLOCK in serialOpen().
I tried testing the system and I’m not sure if the problem is with the xbee’s, the transmitting code, or the recieveing code. Before I post my code I will explain what we are doing with the signals. We have three analog signals that will be sent serially through one xbee using an arduino and xbee shield. We want to send these signals to the receiving xbee where the arduino will output these signals to be connected to a third arduino through wires to be used in a Simulink program. We are using an arduino mega for the transmitting side and an arduino uno for the receiving side. I was told I need to do serial streaming but I’m not sure how that’s done. I understand the xbee and arduinos both digitize signals but we are hoping to get a signal very similar to the analog signals we are transmitting. Any amount of help is greatly appreciated!!
This is how I have my xbees configured (series 1) both in AT mode:
Transmitting Xbee:
Channel:10
Pan id: 1234
MY: 10
DL: 11
Receiving Xbee:
Channel:10
Pan ID: 1234
MY: 11
DL: 10
transmitting Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
// read the input on analog pins
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
// print out the value you read:
Serial.println(sensorValue1);
Serial.println(sensorValue2);
Serial.println(sensorValue3);
delay(1);
}
Receiving Arduino code:
int received1=8;
int received2=9;
int received3=10;
void setup(){
pinMode(received1, OUTPUT);
pinMode(received2, OUTPUT);
pinMode(received3, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available() )
{
byte output1 = Serial.read();
byte output2 = Serial.read();
byte output3 = Serial.read();
digitalWrite(received1, HIGH);
digitalWrite(received2, HIGH);
digitalWrite(received3, HIGH);
}
}
It sounds like you're using the XBee modules in "AT mode" or "transparent serial" mode where anything received on the serial port of module A is sent out of the serial port of module B, and vice versa.
If that's the case, it may help to do your initial development with the serial ports of your two devices connected directly to each other. Work out your serial protocol there, and then try to run it with the XBee modules in place as a serial cable replacement.
Consider the format of the data that you're sending, and how you will process it on the other end. How will you separate the readings and identify which analog input they belong to? With your current code, you print the readings on separate lines, but it won't be clear which is A0. Maybe you want to send them on a single line with a comma in between each reading?
On the receiving end, you need to convert the text back to an integer using a C function like atoi() or strtoul().
If you're trying to create an analog output on the Arduino, it might be possible with a digital output that's using PWM (pulse width modulation). This Instructable does a decent job of describing that concept.
At the moment I'm trying to use the sparkfun promicro to control the RX pin at will using this sketch https://www.sparkfun.com/tutorials/338 .
However I have run into a problem where despite having control over the RX and TX led, it is being interfered by the USBCore.cpp in arduino. I am wondering if there is a clean way to disable control by USBCore over RX and TX pins, while still leaving the USB serial alone, so that I can control these pins directly, even while receiving and sending serial data.
/* Pro Micro Test Code
by: Nathan Seidle
modified by: Jim Lindblom
SparkFun Electronics
date: January 20, 2012
license: Public Domain - please use this code however you'd like.
It's provided as a learning tool.
This code is provided to show how to control the SparkFun
ProMicro's TX and RX LEDs within a sketch. It also serves
to explain the difference between Serial.print() and
Serial1.print().
*/
int RXLED = 17; // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
void setup()
{
pinMode(RXLED, OUTPUT); // Set RX LED as an output
// TX LED is set as an output behind the scenes
Serial.begin(9600); //This pipes to the serial monitor
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}
void loop()
{
Serial.println("Hello world"); // Print "Hello World" to the Serial Monitor
Serial1.println("Hello!"); // Print "Hello!" over hardware UART
digitalWrite(RXLED, HIGH); // set the LED on
TXLED1; //TX LED is not tied to a normally controlled pin
delay(1000); // wait for a second
digitalWrite(RXLED, LOW); // set the LED off
TXLED0;
delay(1000); // wait for a second
}
If there is no way to tackle this cleanly without modifying the arduino enviroment, then I'll shall modify USBCore.cpp . However its probbly bad practice to do so.
If its possible in your scenario, you could use pin 17 as an INPUT, hopefully freeing up another pin which you can then use as an OUTPUT.
To do this, just use pinMode() to set pin 17 to an INPUT.
That effectively disables the RXLED functionality. When USBCore write a high to that pin, it merely turns on the pullup resistor. As long as the device driving the input can sink enough current even when the pullup is on, this will have no effect. And so there is no need to modify USBCore.
Edit: The LED lights up when pin 17 is low, which means the source of the signal needs to sink current. That can be avoided if its an issue by severing the PCB track next to the LED, or by desoldering the LED or resistor.
How do I capture the output from an AT command on an Arduino?
I'm using the Arduino Uno R3 with a GSM shield. I have all the AT commands (they can be seen here ) and I can enter them just fine if I use the terminal and get output. However how can I capture the resulting output via code? The code below shows what I've tried but it does not work. In particular where I attempt to get the analog input and then print out the result.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
void setup()
{
char sensorValue[32] ="";
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("\r");
//Wait for a second while the modem sends an "OK"
delay(1000);
//Because we want to send the SMS in text mode
Serial.println("AT+CMGF=1\r");
delay(1000);
mySerial.println("AT+CADC?"); //Query the analog input for data
Serial.println(Serial.available());
Serial.println(Serial.read()); //Print out result???
//Start accepting the text for the message
//to be sent to the number specified.
//Replace this number with the target mobile number.
Serial.println("AT+CMGS=\"+MSISDN\"\r");
delay(1000);
Serial.println("!"); //The text for the message
delay(1000);
Serial.write(26); //Equivalent to sending Ctrl+Z
}
void loop()
{
/*
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
*/
}
I get the outputs:
AT+CMGF=1
AT+CADC? 21 13
or
AT+CMGF=1
AT+CADC? 18 65
Regardless of changes in my analog source
Take a look at the documentation of the SoftwareSerial read function here.
When you read from the GSM device serial interface, you cannot take for granted that there are bytes to be read on the buffer.
It's very likely that mySerial.read() returns -1 (no bytes available), as Arduino runs that code before the GSM device can provide something on the serial port.
You should use the available function (documentation here) to test the serial interface for incoming bytes. You could use it with a timeout to avoid infinite waiting.
The best thing you could try is to write a separate class to handle serial operations (read, write, timeouts, delays, etc).
Also, I wrote a GPRS driver for Arduino once.
I had a problem with the power supply that required me to install an extra capacitor on the GPRS device and use a power supply with more than 2A of output current.