Hey people. 1st time posting here but been reading here for like 2 years. This is my latest project ( working awesome). Like you see, I am really amateur at coding. My main concerns are "delays" and too many lines for different buzzer events and also too many lines for "scrolling" the "warning". All this makes my code lag way too much by freezing up the Arduino CPU. Yes I have tried Millis and tried to figure out scrolling and routines for my buzzer events. I just cant wrap my head around that. I even tried copy/paste examples I found on internet...and here...and nothing works for me. Feel free to tweak my code. This project ( that works perfect right now)..uses 3 engine sensors from ebay ( still waiting for Temp sensor). It displays Volts, Oil PSI, Fuel PSI and engine Temp. When the values are below limits, warnings alarms turn on. Like I said..pretty amateur coding but I'm 56 and my brain is getting full The car is a 1978 Zephyr. Separate toggle switches to turn on Main Power, Accessories, Ignition, Fuel pump, Start
If you want to suggest modifications, why not, but it must be perfectly detailed. A reply like " add subs and interrupts with quantum extrapolation algorithms" means nothing to me even if I read hours on the subject
Too many lines of code to paste here so here is the link to my code ( Sorry)
BWD
BlinkWithoutDelay, delays created with millis().
Here is a number of examples demonstrated.
//Blink without Delay skeleton
//4 examples demonstrated
//
//LED wiring options
//=============================================
//Depending which way your LEDs are wired, uncomment the next line.
//#define PlusEqualsON
#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON LOW
#define ledOFF HIGH
//=========================
#endif
//switch wiring options
//=============================================
//Depending which way your switches are wired, uncomment the next line.
#define PushEqualsLOW
#ifdef PushEqualsLOW
//pushing the switch makes pin LOW
#define Pushed LOW
#define Released HIGH
//=========================
#else
//pushing the switch makes pin HIGH
#define Pushed HIGH
#define Released LOW
//=========================
#endif
//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;
//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL; //100ms
unsigned long ledOnTime = 5*1000UL; //5 seconds
byte laststartSwitchState = HIGH;
byte buttonState = HIGH;
byte counter = 0;
//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;
const byte startSwitch = 2; //pushed = LOW
const byte testSwitch = 3; //pushed = LOW
//**********************************************************************
void setup()
{
Serial.begin(9600);
digitalWrite(13,ledOFF);
pinMode(13, OUTPUT);
digitalWrite(12,ledOFF);
pinMode(12, OUTPUT);
digitalWrite(11,ledOFF);
pinMode(11, OUTPUT);
digitalWrite(10,ledOFF);
pinMode(10, OUTPUT);
pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
pinMode(testSwitch, INPUT_PULLUP); //pushed = LOW
} // >>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
{
//save the current time
currentMillis = millis();
//************************************* E x a m p l e 1
//toggle pin 13 every 200mS
//has 200ms or more gone by?
if (currentMillis - pin13Millis >= 200UL)
{
//code here runs every 200ms
//get ready for next iteration
pin13Millis = pin13Millis + 200UL;
//toggle pin 13
digitalWrite(13,!digitalRead(13));
}
//************************************* E x a m p l e 2
//at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
//could be used as a powerup reset signal
if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
{
//code here runs for 3 seconds after power up, then stops
digitalWrite(12,ledON);
}
else
{
digitalWrite(12,ledOFF);
//disable further pin 12 control
flag12 = false;
}
//************************************* E x a m p l e 3
//if testSwitch is pushed and released
//pin 11 LED goes ON for 5 seconds, then goes OFF
buttonState = digitalRead(testSwitch);
//are we are allowed to check the switch and is it pressed?
if(flag11 == true && buttonState == Pushed)
{
//enable timing of LED on pin 11
flag11 = false; //false --> timing is enabled
//turn LED ON
digitalWrite(11,ledON);
//record the time LED turned ON
pin11Millis = currentMillis;
}
//are we allowed and is it time to control pin 11
if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
{
//if enabled, code here runs after ledOnTime ms goes by
digitalWrite(11,ledOFF);
//allow switch press detection again
flag11 = true; //true --> switch monitoring is enabled
}
//************************************* E x a m p l e 4
//is it time to check the switches?
//in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
//is it time to check the switches
if (currentMillis - SwitchMillis >= debounceMillis)
{
//code here runs every debounceMillis ms
//get ready for the next iteration
SwitchMillis += debounceMillis;
//go and check the switches
checkSwitches();
}
//*********************************
//put other non-blocking stuff here
//*********************************
} // >>>>>>>>>>>>>> E N D O F l o o p ( ) <<<<<<<<<<<<<<<<<
//======================================================================
// F U N C T I O N S
//======================================================================
//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()
{
//re-usable for all the switches
boolean thisState;
//************************************* E x a m p l e Push ON push OFF (toggle)
//check if this switch has changed state
thisState = digitalRead(startSwitch);
if (thisState != laststartSwitchState)
{
//update the switch state
laststartSwitchState = thisState;
//this switch position has changed so do some stuff
//"HIGH condition code"
//switch went from LOW to HIGH
if(thisState == HIGH)
{
//Do some HIGH switch stuff here
}
//"LOW condition code"
//switch went from HIGH to LOW
else
{
//Do some LOW switch stuff here
digitalWrite(10, !digitalRead(10));
//print number of pushes
counter++;
Serial.println(counter);
}
} //END of startSwitch code
//*****************************************
//similar code for other switches goes here
//*****************************************
} //END of checkSwitches()
//**********************************************************************
//======================================================================
// E N D O F C O D E
//======================================================================
Hey people. 1st time posting here but been reading here for like 2 years. This is my latest project ( working awesome). Like you see, I am really amateur at coding. My main concerns are "delays" and too many lines for different buzzer events and also too many lines for "scrolling" the "warning". All this makes my code lag way too much by freezing up the Arduino CPU time. Yes I have tried Millis and tried to figure out scrolling and routines for my buzzer events. I just cant wrap my head around that. I even tried copy/paste examples I found on internet...and here...and nothing works for me. Feel free to tweak my code. This project ( that works perfect right now)..uses 3 engine sensors from ebay ( still waiting for Temp sensor). It displays Volts, Oil PSI, Fuel PSI and engine Temp. When the values are below limits, warnings alarms turn on. Like I said..pretty amateur coding but I'm 56 and my brain is getting full The car is a 1978 Zephyr. Separate toggle switches to turn on Main Power, Accessories, Ignition, Fuel pump, Start
If you want to suggest modifications, why not, but it must be perfectly detailed. A reply like " add subs and interrupts with quantum extrapolation algorithms" means nothing to me even if I read hours on the subject
Too many lines of code to paste here so here is the link to my code ( Sorry)
Well, you had me worried there as your "Subject" contained the dangerous word - "interrupt(s)".
Fortunately on inspecting your code, that word does not appear, so we are safe.
The obvious blunder in your code is the use of "while" (which coincidentally appears in maroon in my editor). This and "delay()" should not appear; there should be a series of "if" statements.
Your code is turgid. It needs a lot of work to remove the delays; some loops will make it considerably shorter and able to be posted directly in "code" tags. You do need to understand the "Blink without delay" concept before you can proceed.
Some resources to learn about using millis() (and micros()) for timing. Credit to groundFungus for this list.
Here's some code that might do what you are after. It compiles, but I did not actually test it. It may have a bug or two in the scrolling warnings but it should give you the idea of how to do multiple things at the same time and avoid all those delay()
By breaking the code into several short single-purpose functions it will be much easier to develop, test, debug and maintain.
Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.
rotorway133:
Yes I have tried Millis and tried to figure out scrolling and routines for my buzzer events. I just cant wrap my head around that.
I've been saving this for an opportune time, maybe this is it. The image was bigger on my screen. The forum software apparently reduces the size. I can't find the way to make it bigger.
There are multiple errors of " was not declared in this scope". Mostly A14, A13, analogInput…..Tried everything I know to resolve this Its so complicated and out of my comfort zone. There are some great ideas in there but I cannot get those errors resolved first when I compile
There are multiple errors of " was not declared in this scope". Mostly A14, A13, analogInput…..Tried everything I know to resolve this Its so complicated and out of my comfort zone. There are some great ideas in there but I cannot get those errors resolved first when I compile
Make sure you have the arduino IDE set to compile for a MEGA. Tools > Board > Arduino/Genuino Mega or Mega 2560
Incidentally, if you change the input & output pins around a bit (such as using A0, A1, etc instead of A13, A14...), your sketch will easily run on an UNO or nano
My original sketch compiles and works perfectly in my car. Same pins and all. As for changing the pins around....I cannot do that. My Mega pins/wires are all soldered and everything is hot glued is the case. I only have the USB port to access the board now
This tweaked version of my sketch that was done by another person in here, is so complicated I cannot figure out the errors. Looks awesome...but way above my knowledge
That sketch will compile for a MEGA, the errors you are getting are typical for having the wrong board selected, because only the MEGA has those particular input/output pins. The only messages I get during compilation are a couple of warnings about comparing signed and unsigned integers, and a warning about a multi-line comment in the liquidcrystal library, all of which can be ignored (you won't even see those if you don't have the IDE set to show all messages).
It was late and after reading my code, I noticed a few defects.. Updated version attached (still untested since I don't have the hardware).
If it all looks alien to you then you probably need to tackle simpler projects and get proficient before taking on such a project as this. I'm sure the code will require some tweaking to get it to work properly, but I can do that part.
blh64:
It was late and after reading my code, I noticed a few defects.. Updated version attached (still untested since I don't have the hardware).
If it all looks alien to you then you probably need to tackle simpler projects and get proficient before taking on such a project as this. I'm sure the code will require some tweaking to get it to work properly, but I can do that part.
Line 266:
lcd.setCursor(20 - idx, 1);
should be
lcd.setCursor(20 - idx, 2);
Also, the WARNING message is scrolling a bit too fast to read, and would probably work better if it displayed the full word for a short time before starting the scroll again.
EDIT:
You have also defined two variables for pin 3, the actual backlight is bit #3 on the I2C expander for the LCD display. In the original sketch it appears to be for a buzzer, but the use was a bit inconsistent because sometimes it was being used as an on/off type control, and at other times as a tone output.
const int BacklightPin = 3;
...
...
const int BeepPin = 3; // <--- Same pin as Backlight Pin ??????
Yep. I noticed those problems as well. Actually, a better alternative (and easier, more straight forward) to scrolling would be to simply flash the WARNING message. Display it for 500ms, erase it for 500ms, etc.
In some test code I ran, I actually slowed the scrolling down to a delay of ~250msec.
The current code with the backlight flashing on/off based on 3 different warnings does cause a lot a flicker.