Go Down

Topic: Not understanding what is going on here, only using constants... (Read 992 times) previous topic - next topic

Arrch


how would you put the board start code into a function. I need those pin name usable throughout the code.

You should go through the beginning examples to see how the code is organized and put into the proper function.

timberwolf9

#6
Jun 25, 2012, 06:05 pm Last Edit: Jun 25, 2012, 06:21 pm by timberwolf9 Reason: 1


how would you put the board start code into a function. I need those pin name usable throughout the code.

You should go through the beginning examples to see how the code is organized and put into the proper function.


I'll go ahead and do that, the blink example seems rather simple.

Edit: thank you Arrch, here is the fixed code. Obviously I'm just starting out on Arduino, but this should make things a bit easier knowing how to declare functions.

New and Fixed code:
Code: [Select]
/* open source arduino engine control program. Written
and debugged by Bo (las name left out). Made for a go kart being built
and raced by Charles (las name left out). This version, v 1.0, is open
source by the GPL v3 License. */

// state our inputs for the board
int oilTemp = A2;
int airTemp = A3;
int rpmSensor = 4; //will be a inductance coil used for timing fuel
int throttlePosition = A0; //will measure a variable resistor
int sysVoltage = A1; //need anolog input to read voltage
//all inputs for this board have been stated

//state our outputs for the board
int injectorOut = 6; //needs to be on PWM pin for fine grain control
int fanState = 7; //used to turn the oil cooling fan on or off
int ignitionShort = 8; /* used to ground out the firing coil if the engine
exceeds the operating temp threshold. This will effectively stop the
engine from running while overheated. */
int oilTL = 9; //light for engine overheat warning.
//pulses for possible alert, stays on when condtion reached.
int lowVL = 10; //low voltage light, comes on at 10.4 volts
int overRPML = 11; //comes on when engine RPM becomes too high
// int CoilOff = 12 later: brownout state for injector coil
void setup() {
//set inputs
pinMode(oilTemp, INPUT);
pinMode(airTemp, INPUT);

//set outputs
pinMode(injectorOut, OUTPUT);
pinMode(fanState, OUTPUT);
pinMode(ignitionShort, OUTPUT);
pinMode(oilTL, OUTPUT);
pinMode(lowVL, OUTPUT);
pinMode(overRPML, OUTPUT);
}

void loop() {
RunTime();
}

void RunTime() {
  testLights();
}

void testLights() {
//test lights
digitalWrite(oilTL, HIGH);
digitalWrite(lowVL, HIGH);
digitalWrite(overRPML, HIGH);
delay(2500);
//2.5 second test period for testing dummy lights
digitalWrite(oilTL, LOW);
digitalWrite(lowVL, LOW);
digitalWrite(overRPML, LOW);
}
"Hold my rootbeer and watch this" - Charlie and I

JimboZA


I'll go ahead and do that, the blink example seems rather simple.


Yep... simplicity being the point of that example. Simple or not, it shows the make-up of a complete program and the syntax and how to use a number of things. Have a look at the Reference for details. Another good tutorial is here at Ladyada

Edit... I see you fixed it in the meantime  8)
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

timberwolf9

Okay...so now I have run into another problem I am not understanding, I'm using the "if | else" scheme to do simple checks and status changes in my program, only the serial output ONLY show the information header and the "else" part of the dummy lights and EFI serial output data....any reason why?

I've attached the current code below. Yes, I know it is a bit of a train wreck, but it'll get better as a start to get parts in and can do real testing of the whole project. And because I know someone will be curious, yes this project is for building an EFI capable ECU for a racing gokart. Lol :) Thanks to those who help out with this project :)
Code: [Select]
/*  <Bo Lansdale and Charles Klein; PowerHound goKart ECU>
    Copyright (C) <2012>  <Bo Lansdale and Charles Klein>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


/* open source arduino engine control program. Written
and debugged by Bo Lansdale. Made for a go kart being built
and raced by Charles Klein. This version, v 1.0, is open
source by the GPL v3 License.

Copyright 2012 Bo Lansdale and Charles Klein*/

/* please keep in mind that this program/software is NOT built to be fit for any purpose, it is just
a trial-and-error to see if such could even be done with an Arduino. The software was orignally
built for a fully custom goKart making use of a 13HP four-stroke gasoline engine. While you can
use this software for your own projects, we take NO responsibility for what may or will happen.

The software is buggy and may crash or hang without warning, causing anything from, but not limited to,
flooding or overheating of the engine.*/

//I'm using a few outside libraries for various functions of the ECU
#include <MemoryFree.h>

//computer needs direct line to battery, else starter current will reset it, lost variables...can be fixed via a
//330uF capacitor and diode circuit to power the computer for a few seconds during starting...

//state our starting variables; variables can be added and taken as needed, if listed here, they are unviersal to
//ALL functions.
int rpm = 0; //is this engine even running yet? doesn't really matter what you set it to, it'll be overwritten
//soon as the engine is running.
int oiltemp = 0; //haven't read oil temp yet; treated same as rpm reading
int airtemp = 0; //have we even figured out the oil temp?; treated same as rpm reading
int volt = 0; //haven't read voltage yet, read every 10 or so routine run throughs
int system_MaxRPM = 400; //this isn't read as actual RPM, but rather as a volt reference...
int system_MaxOilTemp = 325; //again, not actual, but read as a volt reference...
int system_MaxVoltage = 800; // again, not actual, but read as a volt reference...
int ftime = 10; //ms value for injector timing; should be 10ms during hot weather, or 20ms for cold weather
int testDone_lights = 0; //used for the loop function, if test has already been done, DON'T do it again
int testDone_serialInfo = 0; //should we display the info header from boot? only need to once
int RunTime_started = 0; //this part of the software is reserved for a computer interface...don't know if it'll ever be written
int engineStarted = 0; //have we started the engine, this will be set to 1 if the rpm is picked up over 380 rpm

// state our inputs for the board
int FueloffSet_neg = 4; //used to take f ms from the fuel injector ON (HIGH) time.
int FueloffSet_pos = 5; //used to add 5 ms to the fuel injector ON (HIGH) time.
/* adjusting the fuel injector timing this way can help tweak your engine performance to squeeze
out just a hair more power. Or it can semi-help in the case that the RPM sensor is giving a slightly
offset value. */
int oilTemp = A2; //custom thermosistor module placed in the oil resivor for the engine.
int airTemp = A3; //thermosistor module place inside the intake for collecting air temp and adjusting fuel timing.
int rpmSensor = A4; //will be a inductance coil used for timing fuel.
int throttlePosition = A0; //will measure a variable resistor.
int sysVoltage = A1; //need anolog input to read voltage.
//all inputs for this board have been stated

/*mellow people I luffles my lil big pup and have fun with the program.*/
/*aye babygal, loves you too, and I'm sure they will*/

//state our outputs for the board
int injectorOut = 6; //needs to be on PWM pin for better control control
int fanState = 7; //used to turn the oil cooling fan on or off
int ignitionShort = 8; /* used to ground out the firing coil if the engine
exceeds the operating temp threshold. This will effectively stop the
engine from running while overheated. */
int oilTL = 9; //light for engine overheat warning.
//pulses for possible alert, stays on when condtion reached.
int lowVL = 10; //low voltage light, comes on at 10.4 volts
int overRPML = 11; //comes on when engine RPM becomes too high
int accEnableL = 12; //show when the acc device is enabled, not really needed right now

//begin the serial com port for information to be sent to the port reader

void setup() {
  //set inputs
  pinMode(oilTemp, INPUT);
  pinMode(airTemp, INPUT);

  //set outputs
  pinMode(injectorOut, OUTPUT);
  pinMode(fanState, OUTPUT);
  pinMode(ignitionShort, OUTPUT);
  pinMode(oilTL, OUTPUT);
  pinMode(lowVL, OUTPUT);
  pinMode(overRPML, OUTPUT);
  Serial.begin(9600);
  Serial.print("ECU+INFO= PowerHound Version 1.1: The goKart ECU/EFI system\r\n");
  Serial.print("Built by Bo Lansdale, debugged by Charles Klein!\r\n");
  Serial.print("LN+ Starting system routines...we will output data\r\n");
  Serial.print("to the serial port containing system values as the\r\n");
  Serial.print("engine is running. Values you see automatically update\r\n");
  Serial.print("and are readable as LN+, RT+, or ECU+ command reports.\r\n");
 
  /* as versions change and are updated, the serial protocol will always remain as 9600 baud, but the
  commands may change or new one added. the DI (Device Interface) system will NOT be added to this micro
  version. We will be producing two versions of the PowerHound Open Source ECU. A micro version using
  just the bare bones to manage the engine, and a full version packed with the DI.
  */
}

void loop() {
  testLights();
}


void testLights() {
  //test lights
  if (testDone_lights = 0)
  {
    Serial.print("Initializing Dummy Lights....\r\n");
    digitalWrite(oilTL, HIGH);
    digitalWrite(lowVL, HIGH);
    digitalWrite(overRPML, HIGH);
    delay(2500);
    //2.5 second test period for testing dummy lights
    digitalWrite(oilTL, LOW);
    digitalWrite(lowVL, LOW);
    digitalWrite(overRPML, LOW);
    testDone_lights = 1;
    Serial.print("Passed.....\r\n");
    RunTime();
  }
  else {
    RunTime();
  }
}

void RunTime() {
  if (RunTime_started = 0) {
    Serial.print("Starting RunTime......\r\n");
    //OMG We finally added stuff to the runtime!!! let us show the free memory for the ECU
    Serial.print("freeMemory()=");
    Serial.println(freeMemory());
    EFI();
  }
  else
  {
    EFI();
  }
}
void EFI() {
  if (engineStarted = 0) {
    int rpm = 0;
    int ftime = 10;
    digitalWrite(injectorOut, HIGH);
    delay(ftime);
    Serial.print("ECU+fTime= " + ftime);
    digitalWrite(injectorOut, LOW);

  }
  else
  {

    delay(ftime);
    Serial.print("Checking and adjusting fuel injection.....\r\n");
    pinMode(rpmSensor, INPUT); 
    LightsModule();
  }
}

void LightsModule() {
  Serial.print("Checking statuses for dummy lights....\r\n");
  int Reading_oiltemp = 0;
  Reading_oiltemp = analogRead(oilTemp);
  if (Reading_oiltemp >= system_MaxOilTemp)
  {
    digitalWrite(oilTL, HIGH);
  }
  else
  {
    digitalWrite(oilTL, LOW);
  }
  int Reading_rpmSensor = 0;
  Reading_rpmSensor = analogRead(rpm);
  if (Reading_rpmSensor > system_MaxRPM) {
    digitalWrite(overRPML, HIGH);
  }
  else
  {
    digitalWrite(overRPML, LOW);
  }
}
"Hold my rootbeer and watch this" - Charlie and I

PeterO

"if (testDone_lights = 0)" will always be false.

"=" is the assignment operator.  You need to use "==" which is the comparison operator.

Your if statement reduces to "if(0)" which is always false.

PeterO

Go Up