PWM preset state issues ("not declared in this scope")

Hi everyone,

i am in the middle of designing a fast idle controller for my project car and cannot work out what my issue is, im still pretty novice in programming and this may be a simple issue, could some of the pros on here please look over my code and see if there are any mistakes?

thanks in advance

Mike

//PWM engine idle valve controller by Mike Quirke 2014 "jupiterengineering" is my username on the arduino forum.

//the code below is designed to control a two or three wire air idle valve used in many fuel injected cars.
//operating on PWM, in similar fashion to a servo, the valve opens to various positions, depending on engine conditions to allow throttle
//compensation for heavy electrical or mechanical loads at idle speed, to pevent engine stall should the A/C power up or, in a condition of a
//cold engine where a small percentage of extra throttle is needed .

//the device takes a 5v signal from either, or both of the A/C controller or compressor solenoid and the cold idle pinout from either the cars
//engine management unit, or a seperate temperature sensor and provides throttle compensation based on the state/s or the two inputs.
//if the two inputs are revieving their 5v signal, both duty cycles are summed up to compensate for the load of two situations, otherwise, either function
//as required by conditions.

//i have configured the pins for use with an attiny 85 chip.

#include <Servo.h>
Servo idlevalve; //initiate servo object to control the idle valve
int warmup; //integer (below) for engine warmup with A/C off
int ac; //integer (below) for A/C on, warmup off
int warmupandac; //integer (below) for warmup on, A/C on
const int goingHigh = 920; // goingHigh = 920: 4.50V/5V * 1023 punch this in calculator to verify...
const int goingLow = 102; // goingLow = 102: 0.50V/5V * 1023 punch this in calculator to verify...

void setup() {
idlevalve.attach (0); //PWM output to idle valve driver transistor
pinMode (1,OUTPUT); //led indicating engine warmup mode activated
pinMode (2,OUTPUT); //led indicating A/C compensation mode activated
pinMode (A3,INPUT); //input from ecu to activate fast idle
pinMode (A2,INPUT); //input from A/C system to activate A/C idle compensation
const int warmup= map(warmup,goingLow,goingHigh,0,90); //for warmup on, A/C off,scaling input values to activate idle valve at (90,which is 50 percent duty cycle to warm up engine
const int ac= map(ac,goingLow,goingHigh,0,45); //for A/C on, warmup off (45 or 25 percent duty cycle)
int warmupandac= map(warmupandac,goingLow,goingHigh,0,135); //for warmup on, A/C on (135 or 75 percent duty cycle)
int enginetempStatus = analogRead(A3); //read A3 to determine if warmup mode required or not
int acStatus = analogRead (A2); //read A4 to determine if A/C mode required or not
}

void loop()
{
if (enginetempStatus >= goingHigh){ //if ecu cold idle activated,
digitalWrite (1,HIGH); //turn on warmup mode LED
idlevalve.write(warmup); //also activate idle valve to specified warmup duty cycle.
}
if (acstatus >= goingHigh){ //if A/C is switched on,but warmup is off,
digitalWrite (2,HIGH); //turn on A/C status LED
idlevalve.write(ac); //activate idle valve to specified A/C duty cycle.
}
if (enginetempStatus, acStatus >= goingHigh){
digitalWrite (1,2,HIGH); //turn on both status LED's
idlevalve.write(warmupandac); //activate idle valve to specified A/C duty cycle.
}
delay (1);
}

Arduino: 1.5.6-r2 (Windows XP), Board: "Arduino Mega or Mega 2560, ATmega1280"

idle_controller.ino: In function 'void loop()':
idle_controller:39: error: 'enginetempStatus' was not declared in this scope
idle_controller:43: error: 'acstatus' was not declared in this scope
idle_controller:47: error: 'enginetempStatus' was not declared in this scope
idle_controller:47: error: 'acStatus' was not declared in this scope
C:\Documents and Settings\Administrator\My Documents\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:125: error: too many arguments to function 'void digitalWrite(uint8_t, uint8_t)'
idle_controller:48: error: at this point in file

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

when posting some code, it would be best to post it between CODE tags. You can press the NUMBER SYMBOL ( # ) button up there on the format icons and then insert your code between the tags, something like this:

[ code]
your code here
[ /code]

and it will look like:

your code here

As to your problem, at first glance it seems to me that you are trying to use some variables on your loop() which you declared inside the setup().

When you declare a variable inside a function it will be a local variable, so it will be accessible only inside that function.
For instance your acStatus variable. You are declaring it inside your setup() and trying to use it on your loop(). This will not work.
The way to make it work is to make them global variables. You can declare it before your setup(), up with your other variables.

Also notice that they are case sensitive, so acStatus is different from acstatus.

So, make your acStatus and your enginetempStatus variables global, something like:

int acStatus;
int enginetempStatus       <----  maybe you could write engineTempStatus. helps reading it

Then if you want to use them on your setup () you don't need to declare them again, you can just write something like

acStatus = analogRead (A2);

Good luck!
:wink:

thank you kindly for the reply, i have made the variables global, fixed the case lettering and it seems to check out ok...will test the code on my arduino mega and report back as to how it functions, as it will be a few months until i get my car going...

ok, this is what it looks like now, i have attempted to tidy things up here, however the LEDs dont work as they are supposed to, they stay on all the time, and i only recieve one state of PWM...

is something perhaps wrong with my arduino (mega) board?

#include <Servo.h>
Servo IdleValve; //initiate servo object to control the idle valve
const int GoingHigh = 1000; // goingHigh = 920: 4.50V/5V * 1023 punch this in calculator to verify...
const int GoingLow = 0; // goingLow = 102: 0.50V/5V * 1023 punch this in calculator to verify...
const int WarmUp= map(WarmUp,GoingLow,GoingHigh,0,90); //for warmup on, A/C off,scaling input values to activate idle valve at (90,which is 50 percent duty cycle to warm up engine
const int AC= map(AC,GoingLow,GoingHigh,0,45); //for A/C on, warmup off (45 or 25 percent duty cycle)
const int WarmUpandAC= map(WarmUpandAC,GoingLow,GoingHigh,0,135); //for warmup on, A/C on (135 or 75 percent duty cycle)
const int Off= map(Off,GoingLow,GoingHigh,0,0); //both functions disabled                                                                                                           

void setup() 
{
IdleValve.attach (6); //PWM output to idle valve driver transistor
pinMode (13,OUTPUT); //led indicating engine warmup mode activated
pinMode (22,OUTPUT); //led indicating A/C compensation mode activated
pinMode (A3,INPUT); //input from ecu to activate fast idle
pinMode (A2,INPUT); //input from A/C system to activate A/C idle compensation
}

void loop()
{
int EngineTempStatus = analogRead(A3); //read A3 to determine if warmup mode required or not
int ACStatus = analogRead (A2); //read A4 to determine if A/C mode required or not

if  (ACStatus >= GoingHigh, EngineTempStatus <= GoingHigh) //if ecu cold idle activated,
{
digitalWrite (13,HIGH); //turn on warmup mode LED
digitalWrite(22,LOW);
IdleValve.write(WarmUp); //also activate idle valve to specified warmup duty cycle.
}
else 
{
digitalWrite(13,LOW);
digitalWrite(22,LOW);
IdleValve.write(Off);
}
{
if (ACStatus <= GoingHigh, EngineTempStatus >= GoingHigh)
{ //if A/C is switched on,but warmup is off, 
digitalWrite (22,HIGH); //turn on A/C status LED
digitalWrite(13,LOW);
IdleValve.write(AC); //activate idle valve to specified A/C duty cycle.
}
else 
{
digitalWrite(22,LOW);
digitalWrite(13,LOW);
IdleValve.write(Off);
}
{
if (ACStatus <= GoingHigh, EngineTempStatus <= GoingHigh)
{
digitalWrite(22,HIGH);
digitalWrite(13,HIGH);
}
else
{
digitalWrite(22,LOW);
digitalWrite(13,LOW);
IdleValve.write(Off);
}
{
delay (1);
}
}
}
}

Why do you have an opening bracket before this line:

{
if (ACStatus <= GoingHigh, EngineTempStatus >= GoingHigh)

and another just before:

{
 if (ACStatus <= GoingHigh, EngineTempStatus <= GoingHigh)

and your 1ms delay is also inside brackets

{
   delay (1);
}

?

Hmmm i have never seen an if statement with two conditions separated by a comma. I didn't even know one could do it. How does that work?
If you are checking if both conditions are true, why not use a && (AND) operator?

if(ACStatus <= GoingHigh && EngineTempStatus >= GoingHigh)

You asked if something os wrong with your board because the leds are always on. hmmm, i would say that probably the problem is with the code. If you want to check if the board is working, try writing another very simple sketch just making the leds blink or something...
:wink:

pinMode (A3,INPUT); //input from ecu to activate fast idle
pinMode (A2,INPUT); //input from A/C system to activate A/C idle compensation

The pinMode() function affects digital pins. The pins, as you are using them, are not digital pins, so get rid of this.

if  (ACStatus >= GoingHigh, EngineTempStatus <= GoingHigh) //if ecu cold idle activated,

I am 100% certain that you are misusing the comma operator. I am 0% certain what the hell you are trying to do.

is something perhaps wrong with my arduino (mega) board?

That's a dangerous road to take. Blaming a bug in the compiler is often down that route as well.

Of course, both options are plausible, but generally, the first place to look is your own code. Given your apparent low level of experience with C++, that goes double.

As suggested, if you really think that there's an issue with your mega, write the most trivial sketch you can to prove it.

Thanks to all for the reply,

Yes I have very little experience in coding, in fact I don't consider myself experienced at all...

I will try again to write a new sketch employing the && function, then I will run an example sketch like blink to double check the board, ,however this looks like its my doing and only my doing thats causing the problem.

ok, here is my latest sketch, seems to work fine, at the moment im using a model servo, this operates erratically and different i imagine to a pwm solenoid valve.
led's go on an off as they should, so given the right time, i will build a driver circuit for the valve and see if it works, for now, here is the sketch...

[ code]
const int on = 920; // goingHigh = 920: 4.50V/5V * 1023 punch this in calculator to verify...
int WarmUpPin = A0; // cold idle input from engine management
int ACPin = A1; // air conditioning compressor on input from ac solenoid or controller

void setup()
{
pinMode (3,OUTPUT); //PWM output to idle valve driver
pinMode (5,OUTPUT); //cold idle indicatior led
pinMode (6,OUTPUT); //ac compressor on led

}
void loop()
{
int Engine = analogRead(WarmUpPin); //sample engine management output
int AC= analogRead (ACPin); //sample ac compressor solenoid status

if (Engine >= on && AC <= on) //if engine in warm up mode,
{
digitalWrite (5,HIGH); //activate warmup led
digitalWrite(3,6); //open idle valve to warmup position
}
if (Engine <= on) //if engine at normal running temp,
{
digitalWrite (5,LOW); //turn everything off, unless ac is switched on
digitalWrite(3,0);
}
if (AC >= on && Engine <= on) //if ac is on but engine is at normal running temp,
{
digitalWrite (6,HIGH); //turn on ac indicator led,
digitalWrite(3,3); //move idle valve to ac position
}
if (AC <= on) //if ac off,
{
digitalWrite (6,LOW); //turn everyhting off
digitalWrite(3,0);
}
if (AC >= on && Engine >= on) //if engine in warmup mode with ac on (defrost mode)
{
digitalWrite (5,HIGH); //turn on both leds
digitalWrite (6,HIGH);
digitalWrite(3,10); // move idle valve to wide open position to compensate for the load of both conditions
}
if (AC <= on && Engine <= on) //if both ac and warmup is off,
{
digitalWrite (5,LOW); //turn everything off and let engine idle as normal
digitalWrite (6,LOW);
digitalWrite(3,0);
}
delay (1); //delay for stability
}

[ /code]

digitalWrite(3,6); //open idle valve to warmup position

Six? The second argument to digitalWrite() is HIGH or LOW.

You seem to have some serious misconceptions about what digitalWrite() does on a PWM pin. It still only turns it on or off.

The analogWrite() function, on the other hand...

delay (1); //delay for stability

Nonsense.