About 30% of the time my Serial Monitor prints out something like below. The first line is munged.
Is there anything I can do to make it always start with the first Serial.print line? (Yellow)
BlED Voltage Equals 2.84
Yellow LED Voltage Equals 2.16
Red LED Voltage Equals 2.39
Green Led Voltage Equals 2.76
Blue LED Voltage Equals 2.84
White LED Voltage Equals 2.86
Please school me on how to properly ask a programing question, I just got an UNO and started programing two days ago.
Also, I documented some of the code describing what I think it is doing, if I have any wrong ideas, straighten me out. I probably know what it's doing but may have wrong terminology.
//This program will read the Voltage drop across 5 different color LEDs and print it, once.
int passFlag = 0; // Sets a variable for the if statement
int readPin1=A1;
int readPin2=A2;
int readPin3=A3;
int readPin4=A4;
int readPin5=A5; // Names all the header pins will be read
float V1=0;
float V2=0;
float V3=0;
float V4=0;
float V5=0; // Calculated Voltages of LEDs
int delayT=2000; // sets a delay time variable
int readVal1;
int readVal2;
int readVal3;
int readVal4;
int readVal5; // Names variable where voltage on input pins will be stored
String mystring1="Yellow LED Voltage Equals ";
String mystring2="Red LED Voltage Equals ";
String mystring3="Green Led Voltage Equals ";
String mystring4="Blue LED Voltage Equals ";
String mystring5="White LED Voltage Equals ";
void setup() {
// put your setup code here, to run once:
pinMode(readPin1,INPUT);
pinMode(readPin2,INPUT);
pinMode(readPin3,INPUT);
pinMode(readPin4,INPUT);
pinMode(readPin5,INPUT); // Sets Pins to be Inputs
Serial.begin(9600);
readVal1=analogRead(readPin1);
readVal2=analogRead(readPin2);
readVal3=analogRead(readPin3);
readVal4=analogRead(readPin4);
readVal5=analogRead(readPin5); // Reads the Voltage on LEDs (0 to 1023)
V1=(5./1023.)*readVal1;
V2=(5./1023.)*readVal2;
V3=(5./1023.)*readVal3;
V4=(5./1023.)*readVal4;
V5=(5./1023.)*readVal5; // Converts the Value read to a Voltage and stores it
}
void loop() {
// put your main code here, to run repeatedly:
if (passFlag == 0){ // starts an if and stops program if passFlag doesn't equal 0
Serial.print(mystring1);
Serial.println(V1);
delay(delayT);
Serial.print(mystring2);
Serial.println(V2);
delay(delayT);
Serial.print (mystring3);
Serial.println(V3);
delay(delayT);
Serial.print(mystring4);
Serial.println(V4);
delay(delayT);
Serial.print(mystring5);
Serial.println(V5); // Prints out all the Strings and LED Voltages.
passFlag++; // Increments passFlag number in the if statement
}}
9600 is a very slow baud rate. Suggest using 115200.
String variables can cause memory issue if not used carefully. C "strings" are a better option in most cases. e.g. char mystring1[] ="Yellow LED Voltage Equals ";
Your logic for printing out the values just once, is not really necessary. Just move the statements into setup if you just want them run once.
Be careful using delay statements in your code. They "block" any other code from running... in your case it doesn't really matter but in many situations it will cause problems.
UNO
The 1 Second delay didn't fix it.
It looks like I might have caused the problem by exiting out of the serial monitor before the program had finished.
9600 is a very slow baud rate. Suggest using 115200.
OK, I reset it, but don't know what difference it made?
String variables can cause memory issue if not used carefully. C "strings" are a better option in >most cases. e.g. char mystring1[] ="Yellow LED Voltage Equals ";
OK, I'll look into that.
Your logic for printing out the values just once, is not really necessary. Just move the statements >into setup if you just want them run once.
OK, I had tried that earlier and could not get it to work, I had some code error but could not figure it out. I'll try again and post if I can't make it work
Be careful using delay statements in your code. They "block" any other code from running... in >your case it doesn't really matter but in many situations it will cause problems.
OK, say I'm blinking an LED, without a delay it is to fast, is there another command that can be used?
Douger
Instead of delaying, you use a function called millis(). This returns the number of milliseconds since the Arduino started up. By taking a snap shot of this value, and comparing it to the current value, you can' work out how much time has passed.
I'll look at the blink without delay.
I'm coming back to the improper print,
Blue D Voltage Equals 2.86
Yellow LED Voltage Equals 2.17
Red LED Voltage Equals 2.40
Green LED Voltage Equals 2.78
Blue LED Voltage Equals 2.86
White LED Voltage Equals 2.87
I found it only happens the first time the program is run. The first time you open Serial Monitor it takes a double click, if I fast double click it works correctly, but a slow double click and I get an improper print of the first line. Maybe something in the IDE and not the program?
Douger
Give your code something to munch on before your data.
Also it will hep you keep note of what version of your code you are running, very good practice.