Please help. Having trouble with "if" statements.

Hello all, I'm pretty new to arduino and programming in general. I am current working on a sketch for an RC plane. I have lights for the plane functioning perfectly (it was relatively easy), however now I am trying to make a "rocket igniter" using the arduino. Basically I want the arduino, whenever I provide .39 volts at pin A1 to output voltage at digital pin 6 which will create enough current to heat the rocket igniter. I seem to be missing something to make it work, so any help would be greatly appreciated. Here is the code that I have so far:

int sensorPin1 = A1; // Input for wing missiles from receiver int sensorValue1; // Variable to read the value from analog pin A1 int igniter1 = 6; // Missile igniter connected to digital pin 6

void setup() { pinMode(igniter1, OUTPUT); // sets the digital pin as output }

void loop() { sensorValue1 = analogRead(sensorPin1); // Reads the value from sensorPin1 (A1) if (sensorValue1 > 50); // If sensorValue is greater than 50 (.245V), then... { digitalWrite(igniter1, HIGH); // Set igniter1 to on delay(4000); // 4 second delay digitalWrite(igniter1, LOW); // Set igniter1 to off }


Remove the semicolon from - if (sensorValue1 > 50);

so the line reads

if (sensorValue1 > 50)

I don't know if this will fix your problem, since all you write is that it does not work, but this will correct the if statement

And change :

int sensorPin1 = A1; // remove the A

int sensorPin1 = 1;

The rest of the code should compile and work. But I still don't know if the hardware will work...

Thank you, I actually had it like that, and I was trying different things to try to make it work (which is why I added the ;). However, I just took it out and it seems to work again. I'm beginning to think I'm just going a bit crazy as I've been working on the larger code for a while, but thank you very much for your help.

Techone, I'm confused as to how the arduino tells the difference between digital pins and analog pins if you don't include the 'A' in

"int sensorPin1 = A1;"

or should I just not concern myself with the mysterious inner workings of the arduino?

Is <40mA enough to set off an iginter? Don’t they usually need 6V at highercurrent?
I would think you’d want to control a mosfet that connect one side of the igniter to ground, with the other side at +6 or +12.


I may not be the right person to ask that question. An analog read or write does not required to "setup" like pinMode(). When you need a digital pin, you have to "setup" the pinMode(). The analog read pins are : A0, A1, A2, A3, A4, A5. So when the line analogRead() is execute, the microcontroller expect a voltage between 0 to 5 V at those pins. But those pins can be program as digital pins, just the pins numbers are : 14, 15, 16, 17, 18, 19 so you tell in setup : pinMode(14, OUTPUT) ; or pinMode(14, INPUT); So what about analogWrite() ? Same pins ? NO the analogWrite() pins are : 3,5,6,9,10,11 and you do not setup those pins either if use has analogWrite(). But if you need those pins has digital, well you have to setup those pins.

I hope you follow me so far... I know it sound confusing... and what about your iginter system ? CrossRoads has some concern about it. And he is right, you need a transistor/Mosfet to control the high voltage / current of the igniter system. And I say again, the code should work but I still don't know about the hardware.

Techone, I’m confused as to how the arduino tells the difference between digital pins and analog pins if you don’t include the ‘A’ in

“int sensorPin1 = A1;”

or should I just not concern myself with the mysterious inner workings of the arduino?

Well you are using analogRead. But judging by the code:

int analogRead(uint8_t pin)
	uint8_t low, high;

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	if (pin >= 54) pin -= 54; // allow for channel or pin numbers
	if (pin >= 14) pin -= 14; // allow for channel or pin numbers

… it will accept both.


It just occur to me.

Let look at your statement :

int sensorPin1 = A1;

A1 is not an integer... 1 is. That is why I delete the A in that statement.

There are aliases for all the analog pins. A0-A5 on a Uno, etc.

The analog read pins are : A0, A1, A2, A3, A4, A5.

Looking at the relevant include file (WProgram.h):

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
...  blah blah ...
const static uint8_t A0 = 14;
const static uint8_t A1 = 15;
const static uint8_t A2 = 16;
const static uint8_t A3 = 17;
const static uint8_t A4 = 18;
const static uint8_t A5 = 19;
const static uint8_t A6 = 20;
const static uint8_t A7 = 21;

You can see that A0 is effectively a synonym for 14. So you could do this:

pinMode (A0, OUTPUT);
digitalWrite (A0, HIGH);

That will be identical (on the 328 processors) to:

pinMode (14, OUTPUT);
digitalWrite (14, HIGH);

As I showed above, analogRead simply checks if the pin number is higher than 13, and if so subtracts 14 from it, so you can use analogRead (0) or analogRead (A0) to achieve the same result.

Kind of confusing perhaps, but there you are. :slight_smile: