Hi,
I have a simple sketch (see below) that runs fine on a Leonardo with a VB App that sends simple commands to turn the led on/off and get a reply via serial.
With the Nano Every, the sketch works fine using the IDE Serial Monitor.
It does not work with my VB app: the led does not turn on or off, the read from the serial port only returns one character and that one has nothing to do with what is in the sketch code.
Is this a question of setting better delays or something else?
Thanks
#include <Wire.h>
#define LED_PIN (13)
#define SERIAL_BAUD (9600)
char incoming_byte = 0;
void setup() {
// put your setup code here, to run once:
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
//Serial.flush();
delay(2000);
while(!Serial); //Wait for the serial port connection to open
Serial.println("Setup Complete");
//Serial.flush();
//delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
// Step 1: Just blink the LED. OK, verified this works
// Step 2: increase time to 1 sec and print a status ON or OFF
// Simple Serial1.println() does not output to the serial monitor?
//Got it, I used Serial1 and should have used Serial
//Works for the Loop() but not in the Setup(). Not sure why
//Step 3: Use the VB App.
//Connection Failed in the VB app, but Arduino seems to see it opened.
//Started connecting after WIn11 restart: likely killed whatever kept COM5 open...
//Can receive data through Serial: Rx led flashes and sketch responds
//Cannot send data through Serial: TX led does not flash, and sketch does not hang up
//Serial.flush only works on the sending buffer and its function is to pause the sketch while the send buffer clears
delay(2000);
//digitalWrite(LED_PIN, HIGH);
//delay(1000);
//Serial.println("LED is ON");
//delay(1000);
//digitalWrite(LED_PIN, LOW);
//delay(1000);
//Serial.println("LED is OFF");
//delay(1000);
// Try to see if Arduino responds to a serial input, whatever it is
while(!Serial);
if (Serial.available()>0) {
incoming_byte = Serial.read();
//
if (incoming_byte=='1') {
digitalWrite(LED_PIN, HIGH);
delay(1000);
Serial.println("Led is ON");
delay(1000);
//Serial.flush(); //Wait for the send buffer to clear
}
else if(incoming_byte =='2') {
digitalWrite(LED_PIN, LOW);
Serial.println("Led is OFF");
}
}
//digitalWrite(LED_PIN, HIGH);
//Serial.println("ON");
//delay(4000);
//digitalWrite(LED_PIN, LOW);
//Serial.println("OFF");
//delay(1000);
}