HELP! arduino will not run program unless serial port is open

I have a strange problem. I have written this code to read an IR sensor and give feedback to a vibrational sensor.

i have the problem of when reconnecting the usb to power my microcontroller the code does not start until i either reflash it or open a serial port. i have never had this problem before and am very confused. this is for a college team project and i am very desperate.

here is my code:

const int irSense = A0;
int distance1 = 0;
int distance2 = 0;
int distance3 = 0;

void setup()
{
}

void loop() {

distance1 = analogRead(irSense);

delayMicroseconds(100);

distance2 = analogRead(irSense);

delayMicroseconds(100);

distance3 = analogRead(irSense);

delayMicroseconds(100);

if(distance1 > 475 && distance2 > 475 && distance3 > 475)
{
analogWrite(A2, 200);
delay(1000);
analogWrite(A2, 0);
}

}

any guidance is welcome!

Which board do you use? Probably not a Uno because doing a analogWrite on A2 would be worthless...

And some other things:

  • Post code into code tags, see how to use this forum. Please edit the post.
  • While you're at it, a couple of capitals will not hurt :wink:
  • Once you start numbering variables, arrays, always arrays

I am using an off brand arduino mini. i dont think its the board because i have done some very complex stuff with it and never had a problem.

Really, ignore all the other stuff?

But because it’s a, I guess, Pro Mini it’s basically an Uno so an analogWrite() on A2 is useless. All it does is turn the pin on when you give it a value greater then 128 (from the top of my head). And I don’t even think it sets the pinMode…

Sir my code works just fine. i can measure distances from 10 inches to 150 inches with an accuracy of .5 inch.

Soooo if you are just here to criticize my coding style get lost. yes i know how to use arrays and YES I know that i really should use caps/grammar in my sentences. I just dont bother. I didn’t use arrays because I was playing with the number of readings I wanted to average … for my final code i will use an array to store the values.

I don’t know what you are talking about with the analog pins. I have tried the code with and without setup for those pins and it runs the same either way.

I am trying to figure out why the arduino board is not starting my code until after i either re-flash the code or start a serial monitor.

i actually do not remember the specs of my board off the top of my head. i will have to dig up the receipts but it is basically a chinese knockoff of the nano. I have been using it for quite some time for different projects and have not had this problem.

RW_78:
yes i know how to use arrays and YES I know that i really should use caps/grammar in my sentences. I just dont bother.

Don't

+1 for irony.

"any guidance is welcome!"
@RW_78

You need to read and understand how analogWrite works.
https://www.arduino.cc/en/Reference/AnalogWrite

Please follow these instructions:
http://forum.arduino.cc/index.php/topic,148850.0.html

.

RW_78:
I have a strange problem. I have written this code to read an IR sensor and give feedback to a vibrational sensor.

i have the problem of when reconnecting the usb to power my microcontroller the code does not start until i either reflash it or open a serial port. i have never had this problem before and am very confused. this is for a college team project and i am very desperate.

here is my code:

const int irSense = A0;
int distance1 = 0;
int distance2 = 0;
int distance3 = 0;

void setup()
{
}

void loop() {

distance1 = analogRead(irSense);

delayMicroseconds(100);

distance2 = analogRead(irSense);

delayMicroseconds(100);

distance3 = analogRead(irSense);

delayMicroseconds(100);

if(distance1 > 475 && distance2 > 475 && distance3 > 475)
{
analogWrite(A2, 200);
delay(1000);
analogWrite(A2, 0);
}

}

any guidance is welcome!

I suspect you will get not help until you provide some proper details of exactly what the problem is.

You say it does not work until you 'open a serial port'. So where is this serial port being opened, on a PC next door or down the road ?

Ok here is what is happening:

  1. Flash the program to the board. The program functions within its parameters no problems.

  2. Power down the board (unplug micro USB)

  3. wait....

  4. Power the board on (via USB or Battery )

  5. Nothing happens.

  6. I try resetting the board (doesn't do anything)

  7. I then:
    A) Reflash the board via USB using the Arduino IDE. OR
    B) Open the serial port in the Arduino IDE

Both A and B start the program. The program will not start when the microcontroller is powered on.

This microcontroller is connected to a vibrational feedback system i have built so i can very easily determine if the program is running or not.

I think that covers everything.

Everyone keeps saying that I have not initialized the A0 pin properly. Can this effect the startup of the program. It does not seem to effect my data from my sensor in any way....

I apologize in advance for any grammatical errors in this post.
DO NOT FEEL THE NEED TO POINT THEM OUT

I thought Arduinos Minis or Pro Minis did not have a USB connectors ?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

I don't know what you are talking about with the analog pins. I have tried the code with and without setup for those pins and it runs the same either way.

THEN YOU NEED TO READ;
https://www.arduino.cc/en/Reference/AnalogWrite

If you read it, it will tell you that only certain pins can be used for PWM.

When your code is working, when you analogWrite 200 to A2, what signal do you get on pin A2.
Have you measured it with a DMM.

Thanks.. Tom.. :slight_smile:

Thanks for your reply.

No i have no idea what signal i get. However it does activate the vibrational sensor and i can adjust the intensity of the vibrations with the PWM. so i assume it is working on that pin.

-RW

Hi,
Can I suggest you change the analogWrite to a PWM pin and see if your code starting problem still occurs.

How have you got the piezo connected, a circuit diagram will help.

Tom.... :slight_smile:

Pin A2 is not on a timer pwm pin. this is what analogWrite() becomes. Refer to wiring_analog.c

case NOT_ON_TIMER:
 default:
 if (val < 128) {
 digitalWrite(pin, LOW);
 } 
       else {digitalWrite(pin, HIGH);
 }

I don’t know about what the vibrational sensor attached to A2 requires, but the signal it will get is
5v for 1 second, then it will go low. Then on the next pass thru loop the three analogRead() and delayMicroseconds(100) commands will take about 600 microseconds. Then the pin will go high again if the distance criteria are met.

  1. Nothing happens.

How do you define that? By output to the vibrational sensor?

Why don’t you add some code for led 13 into the code and see if it starts with the power.
Or change to a pwm pin. I agree with Tom George who just posted ahead of me.

That fact that the reset button doesn’t work is a good clue. Do you see led 13 flash when you push the reset?

ok i will try changing the A0 pin to an actual PWM pin. I will also try adding led13 so i know if it is the feedback sensor is actually working.

By nothing happens i mean that the vibrational feedback does not activate. i assumed that as it works in the said conditions the feedback was not the issue. i will play with that and get back to you

the reset button appears to work as the LED flashes when it is pressed.

RW_78:
Sir my code works just fine. i can measure distances from 10 inches to 150 inches with an accuracy of .5 inch.

Never said you couldn't... Even never said anything about reading. Only told you an analogWrite() on a non-PWM pin isn't very useful :wink:

RW_78:
Soooo if you are just here to criticize my coding style get lost. yes i know how to use arrays and YES I know that i really should use caps/grammar in my sentences. I just dont bother.

Please do. It's YOU who wants help. And the least YOU can do is at least make your text readable. I don't care about grammar errors but this is just plain laziness. Same goes for still not editing you start post...

And all I said about you're code style was the lack of arrays give you a lot of extra typing. :wink:

RW_78:
I didn't use arrays because I was playing with the number of readings I wanted to average ...

I miss the relationship completely..

RW_78:
I don't know what you are talking about with the analog pins. I have tried the code with and without setup for those pins and it runs the same either way.

Maybe analogWrite() sets the TRIS/pinMode if you call it on a digital pin. But if it doesn't, the pin never becomes an output. But because of your rudeness I'm to lazy to check if analogWrite() does it.

Ah, cattledog did do it. It doesn't make it an output. So now it simply toggles on and off the pull up :wink:

RW_78:
but it is basically a chinese knockoff of the nano.

Now all of a sudden it's a Nano? I give up...

But a simple question, have you checked the board with a simple sketch? Let's say blink. Does that also not start?

Crap, now I did help...

Ah, cattledog did do it. It doesn’t make it an output. So now it simply toggles on and off the pull up

No, it does set the pin as an OUTPUT. Here’s the entire function from wiring_analog.c

void analogWrite(uint8_t pin, int val)
{
	// We need to make sure the PWM output is enabled for those pins
	// that support it, as we turn it off when digitally reading or
	// writing with them.  Also, make sure the pin is in output mode
	// for consistenty with Wiring, which doesn't require a pinMode
	// call for the analog output pins.
	pinMode(pin, OUTPUT);
	if (val == 0)
	{
		digitalWrite(pin, LOW);
	}
	else if (val == 255)
	{
		digitalWrite(pin, HIGH);
	}
	else
	{
		switch(digitalPinToTimer(pin))
		{
			// XXX fix needed for atmega8
			#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
			case TIMER0A:
				// connect pwm to pin on timer 0
				sbi(TCCR0, COM00);
				OCR0 = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR0A) && defined(COM0A1)
			case TIMER0A:
				// connect pwm to pin on timer 0, channel A
				sbi(TCCR0A, COM0A1);
				OCR0A = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR0A) && defined(COM0B1)
			case TIMER0B:
				// connect pwm to pin on timer 0, channel B
				sbi(TCCR0A, COM0B1);
				OCR0B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR1A) && defined(COM1A1)
			case TIMER1A:
				// connect pwm to pin on timer 1, channel A
				sbi(TCCR1A, COM1A1);
				OCR1A = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR1A) && defined(COM1B1)
			case TIMER1B:
				// connect pwm to pin on timer 1, channel B
				sbi(TCCR1A, COM1B1);
				OCR1B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR1A) && defined(COM1C1)
			case TIMER1C:
				// connect pwm to pin on timer 1, channel B
				sbi(TCCR1A, COM1C1);
				OCR1C = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR2) && defined(COM21)
			case TIMER2:
				// connect pwm to pin on timer 2
				sbi(TCCR2, COM21);
				OCR2 = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR2A) && defined(COM2A1)
			case TIMER2A:
				// connect pwm to pin on timer 2, channel A
				sbi(TCCR2A, COM2A1);
				OCR2A = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR2A) && defined(COM2B1)
			case TIMER2B:
				// connect pwm to pin on timer 2, channel B
				sbi(TCCR2A, COM2B1);
				OCR2B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR3A) && defined(COM3A1)
			case TIMER3A:
				// connect pwm to pin on timer 3, channel A
				sbi(TCCR3A, COM3A1);
				OCR3A = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR3A) && defined(COM3B1)
			case TIMER3B:
				// connect pwm to pin on timer 3, channel B
				sbi(TCCR3A, COM3B1);
				OCR3B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR3A) && defined(COM3C1)
			case TIMER3C:
				// connect pwm to pin on timer 3, channel C
				sbi(TCCR3A, COM3C1);
				OCR3C = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR4A)
			case TIMER4A:
				//connect pwm to pin on timer 4, channel A
				sbi(TCCR4A, COM4A1);
				#if defined(COM4A0)		// only used on 32U4
				cbi(TCCR4A, COM4A0);
				#endif
				OCR4A = val;	// set pwm duty
				break;
			#endif
			
			#if defined(TCCR4A) && defined(COM4B1)
			case TIMER4B:
				// connect pwm to pin on timer 4, channel B
				sbi(TCCR4A, COM4B1);
				OCR4B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR4A) && defined(COM4C1)
			case TIMER4C:
				// connect pwm to pin on timer 4, channel C
				sbi(TCCR4A, COM4C1);
				OCR4C = val; // set pwm duty
				break;
			#endif
				
			#if defined(TCCR4C) && defined(COM4D1)
			case TIMER4D:				
				// connect pwm to pin on timer 4, channel D
				sbi(TCCR4C, COM4D1);
				#if defined(COM4D0)		// only used on 32U4
				cbi(TCCR4C, COM4D0);
				#endif
				OCR4D = val;	// set pwm duty
				break;
			#endif

							
			#if defined(TCCR5A) && defined(COM5A1)
			case TIMER5A:
				// connect pwm to pin on timer 5, channel A
				sbi(TCCR5A, COM5A1);
				OCR5A = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR5A) && defined(COM5B1)
			case TIMER5B:
				// connect pwm to pin on timer 5, channel B
				sbi(TCCR5A, COM5B1);
				OCR5B = val; // set pwm duty
				break;
			#endif

			#if defined(TCCR5A) && defined(COM5C1)
			case TIMER5C:
				// connect pwm to pin on timer 5, channel C
				sbi(TCCR5A, COM5C1);
				OCR5C = val; // set pwm duty
				break;
			#endif

			case NOT_ON_TIMER:
			default:
				if (val < 128) {
					digitalWrite(pin, LOW);
				} else {
					digitalWrite(pin, HIGH);
				}
		}
	}
}

Ah, yeay. You’re right. That’s the problem with snippets :stuck_out_tongue:

Update:

I fixed my problem It wasn't code related at all. I got my hands on the setup from my team and they had wired the 9V battery into the "POW" pins on the board. this was specifically for 5V power sources so there was no regulator for those pins. The board was overheating and apparently crashing. I rewired it and put the battery on a regulated V_in pin.

The program now works flawlessly.

Thanks everybody for your help.

Hi,
What output do you get on A2 or have you changed that?
Measure with a DMM.

Tom... :slight_smile: