I am having a very bizarre serial port communication problem. I have written the sketch below which monitors a small voltage input from two 3VDC solar cells on analog ports 1 and 2. if the voltage difference between the solar cells is more than 30 one way, it brings digital pin 2 low, if more than 30 the other way, it brings digital pin 3 low. Digital pins 2 and 3 go to a normal 8 relay 5V input relay board. I then use a 9VDC power supply switched through the contacts of the relays to drive a motor one way or another. I connect a UNO R3 board to a computer and upload the sketch with no problem. I then start the serial monitor and it shows me the delta between the two voltages on analog pins 1 and 2. I can shade one solar cell and the delta changes accordingly. Everything works fine and as expected until every time I turn on the 9VDC power supply. Once the motor fires up to move back and forth I loose the serial communication to the arduino board. The program continues to run and the motor works fine but every time I turn the 9vdc power supply on I either get the "serial port in use" or the computer totally forgets the serial port assignment. This is readily repeatable with two different arduino boards and three different computers (two XP and one Windows 7). Since the 9VDC running through the relay contacts is totally isolated from the arduino board and associated electronics, this makes no sense at all to me. Once I reboot the computer everything is back to normal until I power up the 9VDC power supply. HELP......
int analogPin1 = 1; // read voltage for UP
int analogPin2 = 2; // read voltage for DOWN
int upPin = 2;
int downPin = 3;
int up = 0; // variable to store the value read
int down = 0;
long lastMillis=0; // for serial write timer
void setup()
{
Serial.begin(9600); // setup serial
pinMode(upPin, OUTPUT);
pinMode(downPin, OUTPUT);
digitalWrite(upPin, HIGH);
digitalWrite(downPin, HIGH);
}
void loop()
{
up = analogRead(analogPin1); // read the input pin
down = analogRead(analogPin2);
if (millis()>lastMillis+1000) {
Serial.print(" up-down delta= ");
Serial.print(up-down);
Serial.println(" ");
lastMillis=millis();
}
// UP PIN LINES --------------------------------------------------------------------------
if ((up-(down))>30) {digitalWrite(upPin, LOW);} else {digitalWrite(upPin, HIGH);}; //move up
if (((down)-up)>30) {digitalWrite(downPin, LOW);} else {digitalWrite(downPin, HIGH);}; //move down
}
Thanks,
Harry
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.