I want my Arduino to light up the LED if he reads "on" in the Serial Port.
At Serial.print(serialData); it prints out what he reads but at if (serialData == "on") it wont work.
int led1 = 9;
int led2 = 6;
String serialData;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
Serial.setTimeout(10);
}
void loop() {
if (Serial.available() > 0){
serialData = Serial.readString();
Serial.print(serialData);
if (serialData == "on"){
analogWrite(led1, 255);
}
if (serialData == "off"){
analogWrite(led1, 0);
}
}
}
Anybody know, what I'm doing wrong?
There are two issues in your code:
The timeout is set to 10ms. In 10ms, you can at best enter a single character. readString() will return after a single character and the read string will likely be "o", "n", "f".
When you hit the RETURN key, a carriage return and a line feed character are also transmitted ("\r\n").
The solution is to increase the timeout considerably and to use readStringUntil() to read until the newline character is discovered. This is the indication that a full word (or command) has been entered.
Additionally, the carriage return and line feed need to be trimmed off.
#include <Arduino.h>
int led1 = 9;
int led2 = 6;
String serialData;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
Serial.setTimeout(2000);
}
void loop() {
if (Serial.available() > 0){
serialData = Serial.readStringUntil('\n');
serialData.trim();
Serial.println(serialData);
if (serialData == "on"){
analogWrite(led1, 255);
}
if (serialData == "off"){
analogWrite(led1, 0);
}
}
}
Related
I am new to this shift register. So i created a online simulation of the shift register that hook up to 8 LEDs, the ol' 8bit translate to 8 LED experiment. My design is that when i entered a character like "a" into the serial monitor, it will show in the result in code like 100000, and it should shown in the LEDs too(the sixth LED should light up).
char character;
byte input = 0b01;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
byte input = 0b01;
if (Serial.available() > 0) {
character = Serial.read();
if (character == 'a') {
Serial.print("character received: ");
Serial.println(character);
for (int i = 0; i < 5; i++) {
updateShiftRegister(input);
input = input << 1;
delay(100);
}
Serial.print("Input: ");
Serial.print(input, BIN);
Serial.println();
} else {
Serial.print("Please retry.\n");
}
}
}
void updateShiftRegister(byte input) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, input);
digitalWrite(latchPin, HIGH);
}
Problem is when i run the thing, the result is alright but the shift register in the simulation is wack. Every LEDs turn on,
I found the shiftout function is main cause of this, any idea why it is causing every LED to light up?
I am new to programming and Arduino.
Board - ESP8266 Nodemcu pinout as below,
What I am trying to achieve is send a command based LOW/HIGH value from pin 0.
A two leg switch's one leg is connected to D3 (GPIO0 and in program 0) and other to ground.
The code I am trying is below,
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
byte exampleButtonAvalue = digitalRead(exampleButtonA);
switch(exampleButtonAvalue)
{
case LOW:
Serial.println("ON IT IS");
break;
case HIGH:
Serial.println("OFF IT IS");
break;
default:
Serial.println("error!");
break;
}
}
Issue I am facing is, when I flash this program, Based on physical switch on or off
It continually prints either "ON IT IS" or "OFF IT IS"
The break is really not happening. I only want it to execute once.
I also tried this with if else and face same problem of repeated printing.
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
if(digitalRead(exampleButtonA) == LOW){
Serial.println("ON");
delay(200);
}
else {
Serial.println("OFF");
delay(200);
}
}
Any assistance?
If you want to see message only once you need to write youre code in setup() section.
All code in loop() section is repeated in loop.
Replace you code with this:
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
int exampleButtonAvalue = 0;
int saved_exampleButtonAvalue = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
exampleButtonAvalue = digitalRead(exampleButtonA);
if (exampleButtonAvalue != saved_exampleButtonAvalue){
if(exampleButtonAvalue == LOW){
Serial.println("ON");
} else {
Serial.println("OFF");
}
saved_exampleButtonAvalue = exampleButtonAvalue;
}
delay(200);
}
I'm trying to take an input from the Arduino serial connection and write that to a file on the IoT2020. The code I'm using testing with is bellow.
The issue I'm having is that the code does not appear to recognize that there is an input and nothing imputed from the serial appears in the file. I even added an LED "notification" to turn on when a new line is detected in the interrupt but it never turns on. This would mean there is something intrinsically flawed in my code. Any help would be great.
//File handle creation
FILE *testFile;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char buff[400];
void setup()
{
// sleep(3);
Serial.begin(115200);
pinMode(6, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
// reserve 200 bytes for the inputString:
inputString.reserve(400);
// Create files
system("touch /media/test.txt");
}
void loop()
{
digitalWrite(6, LOW);
delay(1000);
testFile = fopen("/media/test.txt", "a+");
fprintf(testFile,"This is a great test that has just started \n\r");
fclose(testFile);
while(1)
{
digitalWrite(6, HIGH);
// digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(6, LOW);
// digitalWrite(LED_BUILTIN, LOW);
delay(5000);
if (stringComplete) {
// clear the string:
inputString = "";
stringComplete = false;
testFile = fopen("/media/test.txt", "a+");
//if the file opened okay, write to it:
if (testFile)
{
fprintf(testFile,"This is a great test");
Serial.println(inputString);
inputString.toCharArray(buff, inputString.length());
fprintf(testFile,buff);
// digitalWrite(6, LOW);
// close the file
fclose(testFile);
}
else
{
// if the file didn't open, print an error
Serial.println("error opening test.txt");
}
}
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
digitalWrite(6, HIGH);
stringComplete = true;
}
}
}
I'm trying to send text over the serial monitor using RadioHead ASK. Text input from the serial monitor is not sent to the receiver. I have read up on C++ theory with char arrays and pointers... it's not computing in my head :). How can *msg exist without first declaring char msg? Please see the sample below. It would be great if you could explain the theory with any sample solution. Thank you for your help!
void setup() {
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
else
Serial.println("TX");
}
void loop() {
const char *msg = Serial.read();
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
This seems to work. strMsg.toCharArray(msg, i); Can you please comment on the efficiency of the code? Is there a better way? Thanks!
void setup() {
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
else
Serial.println("TX");
}
void loop() {
if (Serial.available() > 0)
{
String strMsg = "";
int i;
strMsg = Serial.readString();
i = (strMsg.length() + 1);
char msg[i] = {};
Serial.print("Sent: ");
Serial.println(strMsg);
Serial.print("Size: ");
Serial.println(i);
strMsg.toCharArray(msg, i);
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
}
#include <stdio.h>
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() == 4) {
char command[5];
for (int i = 0; i < 4; i++) command[i] = Serial.read();
command[4] = '\0';
Serial.println(command);
if (strcmp(command, "AAAA") == 0) {
digitalWrite(LED, HIGH);
Serial.println("LED13 is ON");
} else if (strcmp(command, "BBBB") == 0) {
digitalWrite(LED, LOW);
Serial.println("LED13 is OFF");
}
}
}
I have that code, that reads 4 characters long strings. However, I need it to ignore any string that is not 4 characters long.
So, imagine this input:
AAAA
BBBB
BBB
AAAA
Right now, it reads {"AAAA", "BBBB", "BBBA"}.
I need it to read {"AAAA", "BBBB", "AAAA"}.
Any idea? Thank you.
You can check the duration time of inter-character delay. Set a timeout, such as 100ms. When there is no more data received after the specified timeout, that means the whole string is transferred completely. Then you can check the length of the string and execute your application logic.