i am reading an analog signal (fuel sensor) on my mega2560 mini. The signal is jumpy so I take the average, serial print it and use that value to write the code. All is working fine if the USB is connected. When I disconnect the laptop and let the Mega run on its own power supply, the code isn't working.
Is this because the laptop isn't connected and therefor the "average" in the serial print can't be read? could not find any thing on google that could help.
any help/ explanation regarding this is welcome
Niels`
int fuel = 4; //pwm pin
const int numReadings = 1000;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A12;
void setup() {
Serial.begin(9600);
Serial.println("Serial port");
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
Serial.print("average: ");
Serial.println(average);
if (average <3) {
analogWrite(fuel, 200);
}else if (average ==3) {
analogWrite(fuel, 160);
}else if (average ==4) {
analogWrite(fuel, 125);
}else if (average ==5) {
analogWrite(fuel, 100);
}else if (average ==6) {
analogWrite(fuel, 80);
}else if (average ==7) {
analogWrite(fuel, 70);
}else if (average ==8) {
analogWrite(fuel, 60);
}else if (average ==9) {
analogWrite(fuel, 55);
}else if (average ==10) {
analogWrite(fuel, 52);
}else if (average ==11) {
analogWrite(fuel, 35);
}else if (average ==12) {
analogWrite(fuel, 20);
}else if (average >=13) {
analogWrite(fuel, 10);
}
}
Lab power supply set to 9v.
the posted code is a part of more code, which is working as it should.
What I am questioning is: can the Arduino work with the serial print data when its not connected with USB to IDE? or do I need to return that value an other way so it can be used when the Arduino is "standalone" ?
The Arduino will happily print to a disconnected Serial port. The mistake that I have seen several times on the forum is for a sketch to contain a line like
while (!Serial);
This will wait forever for Serial to connect which, if the project has no USB connection, then it never will.
Arduino will not work with the serial print data when its not connected with USB to IDE. it need to connect to PC or laptop to use its USB to serial converter to communicate with the board to PC. if u want to use serial print data or any serial comunication u need to connect it to PC via USB. external power supply will run ur programme but fails to work with serial communication...
+1 more for post #6. The Mega will happily send serial data out to a disconnected pin and never know the difference. It has no way of knowing if there is any receiver out there. The serial writes are probably not the issue.
And to be more specific. Lm for power, ground and car ground to Arduino. 5v to the fuel sender and the return of the 2 wire sender to the analog pin. Pwm pin to the fuel gauge.
As I said all works as long the laptop is connected to the Arduino by usb.
So to me, with my limited understanding of Arduino, it’s either power or ground or the serial communication can’t be used if there is no pc connected
It's not that. I've got tons of projects that run just fine while sending serial to nowhere.
How do you have the 9V connected to the Arduino. Are you using the barrel jack? The VIN pin? How do you have it connected? Can you show a schematic?
Yes. And if you'll show a schematic someone might be able to spot the problem.
While the car is running? That might be your problem. Using power from a car's 12V system usually requires a little extra care. That is some pretty dirty power and you'll need to smooth it out a little for the Arduino.
Don't know how I can make a Schematic of all and post it.
But the car is not running and the 9v is on the VIN pin.
If serial com is possible without a pc connected, the problem needs to lay in the wiring/ connection. I did notice a couple of things: usb was still in the mega mini but not in pc, removed it and it seems it made a small improvement, gauge needle was doing something and then stopped. Other thing was, the usb connector on the mega mini is getting hot and so is the LM9V, which is getting even hotter. I know they can become, since the need to dissipate 4V.
i will put on a new mega mini and power it with a buck converter and check if this makes a difference.
You can easily convince yourself that this is not the problem. There are only 4 lines in your code relating to serial communication. Remove those and try again as a test. I predict that you'll see the same behavior as before.