Using Int
Using Int
void loop(){
CurrTime=millis();
/disabling outputs/
}
Now when i use unsigned long for the variables, the code runs very slow, the outputs are acting
funny, dropping out, etc.
I have tried using int, it runs perfectly even up to 4200 RPM, the max rpm of my drill that i use to
test it.
If i use unsigned int, it behaves exactly like when using unsigned long.
Video:
unsigned long/int: unsigned long - YouTube
int: integer - YouTube
The three leds represent the outputs.
Is this simple math really this slow when using unsigned or long variables?
Can cause any problems if i use normal int for storing the millis() output?
neubauerp:
Now when i use unsigned long for the variables, the code runs very slow, the outputs are acting
funny, dropping out, etc.
I have tried using int, it runs perfectly
It can't be caused by difference between unsigned int and int, it more probable that you have an
error(s) in your code.
Please show cookies
We use 🍪
your sketch in full.
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
neubauerp
adjust this in Cookie Settings or learn more by reading our cookie policy. Des '22 post #3
#include <digitalWriteFast.h>
const int ident = 6; //pin 6 = cylinder identification input
const int cyl = 7; //pin 7 = cylinder strobe input
ONLY REQUIRED ACCEPT ALL
Lewatiint
const ke konten
cyl14 utama
= 2; //pin 2 = 1-4 cylinder output
const int cyl52 = 3; //pin 3 = 5-2 cylinder output
const int cyl36 = 4; //pin 4 = 6-3 cylinder output
int identState = 0;
int cylState = 0;
int LastIdentState = 0;
int LastCylState = 0;
bool sync = 0;
int counterCyl = 0;
int counterIdent = 0;
int CurrTime;
int StrobeTime;
void setup() {
//define IO pins
pinModeFast(cyl14, OUTPUT);
pinModeFast(cyl52, OUTPUT);
pinModeFast(cyl36, OUTPUT);
pinModeFast(ident, INPUT);
pinModeFast(cyl, INPUT);
Serial.begin(2000000);
}
void loop() {
identState = digitalReadFast(ident);
if (identState != LastIdentState) {
if (identState == HIGH) {
counterIdent++;
counterCyl = 1;
} else {
counterIdent++;
counterCyl = 4;
}
}
LastIdentState = identState;
if (counterIdent > 1) {
sync = true;
} else {
sync = false;
}
if (counterIdent > 4) {
counterIdent = 2;
}
cylState = digitalReadFast(cyl);
CurrTime = millis();
We use cookies
if (cylState != HIGH) {
🍪
if (cylState != LastCylState) {
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
counterCyl++;
adjust thisSerial.print("current
in Cookie Settings or learn more bycylinder:
reading our cookie");policy.
Serial.println(counterCyl);
StrobeTime = millis();
}
Lewati
} ke konten utama
LastCylState = cylState;
counterIdent = 0;
}
if (counterCyl > 6) {
counterCyl = 1;
}
digitalWriteFast(cyl14, HIGH);
} else {
digitalWriteFast(cyl14, LOW);
}
if ((counterCyl == 5 || counterCyl == 2) && sync == true) {
digitalWriteFast(cyl52, HIGH);
} else {
digitalWriteFast(cyl52, LOW);
}
if ((counterCyl == 3 || counterCyl == 6) && sync == true) {
digitalWriteFast(cyl36, HIGH);
} else {
digitalWriteFast(cyl36, LOW);
}
//end of loop
}
neubauerp:
if (CurrTime - StrobeTime > 500) {
Well if StrobeTime > CurrTime even for 1ms this expression will return true if you use unsigned long
and false if you use int.
neubauerp:
Is this simple math really this slow when using unsigned or long variables?
INT is either 16-bit or 32-bit, depending upon hardware architecture: UNO, Mega, etc. being 8-bit uC
but C/C++ does the 16-bit integer manipulation. Due and other more advanced microcontrollers are
32-bit architecture and C++ honors that native datatype.
We use cookies
this fundamental area.
🍪
Other casts in C++ are very efficient, a few clock cycles used only. Compilers are very optimized in
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
I would not think your experience is indicative of INT byte-size or format.
adjust this in Cookie Settings or learn more by reading our cookie policy.
It would be simple to test in loop() by just capturing micros() at the beginning, perform some integer
math in a do-while loop, and capturing the elapsed time using micros before the close of loop().
Maybe to the inner loop for 10^4 - 10^5 times just to get a decent interval.
Lewati ke konten utama
Repeat above only with an unsigned int.
Compare. And please post the results if you perform the experiment. Your current sketch is not
provided, so further help is not possible.
UPDATE
See you posted your code and others are offering assistance.
killzone_kid:
Well if StrobeTime > CurrTime even for 1ms this expression will return true if you use unsigned
long and false if you use int.
So basically i have created a scenario, when StrobeTime > CurrTime can be true when using
unsigned long, and this causes the errors?
b707:
You never update StrobeTime , so your timers runs only once after program statrts.
But if you using int for timer, it overflow after 32 seconds and the program start you logic from
the beginning
if (cylState != HIGH) {`
is true?
mrburnette:
It would be simple to test in loop() by just capturing micros() at the beginning, perform some
integer math in a do-while loop, and capturing the elapsed time using micros before the close of
loop(). Maybe to the inner loop for 10^4 - 10^5 times just to get a decent interval.
Repeat above only with an unsigned int.
Sorry but i have just picked up an arduino a week ago, this is the first ever program that i wrote in
my life, sooo.. I dont really understand what you are saying
neubauerp:
Isnt StrobeTime being updated every time when
please point the exact line of the code where you updated it.
mrburnette:
We
INT use cookies 🍪
INT
Ouror int? use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
websites
adjust this in Cookie Settings or learn more by reading our cookie policy.
GolamMostafa:
INT or int?
Seriously?
in code, int
in a paragraph to denote emphasis:
INT
INT
int
You struck a pet-peeve, pet peeve at DuckDuckGo
We use cookies
killzone_kid
Faraday
🍪 Des '22 post #16
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
neubauerp:
adjust this in Cookie Settings or learn more by reading our cookie policy.
I don’t know, and tbh don’t want to know, but you were looking for difference in behaviour when
Lewati ke
changing varkonten utama
type, feel free to investigate
gcjr Des '22 post #17
it looks ident rises just before the cylinder pulse and could be used to set the cylinder count to zero
because the cylinder input would rise just after it and set it to 1
i think you're using sync as a flag to indicate the events are occurring and can toggles the LEDs and
flag is reset after a timeout to inhibit toggling LEDs after a timeout period.
mrburnette:
You struck a pet-peeve, pet peeve at DuckDuckGo
It has nothing to do with personal liking/disliking; it is the Compiler that puts objection against the
usage of INT.
GolamMostafa:
It has nothing to do with personal liking/disliking; it is the Compiler that puts objection against
the usage of INT.
Rather it has everything to do with petty responses to how written communications are utilized. Not
only wasting my time but yours; my time is better utilized to go for a fresh cup of coffee.
mrburnette:
Rather it has everything to do with petty responses to how written communications are utilized.
That means that the written communication can violate C++ Language's established conventions?
neubauerp:
The code needs to run pretty fast, as it handles two inputs of a camshaft sensor, one is 1
cycle/rotation, the other is 6 cycle/rotation. Expected max RPM is 3750.
#include <digitalWriteFast.h>
const int PinIdent = 6; //pin 6 = cylinder identification input
const int PinCyl = 7; //pin 7 = cylinder strobe input
byte identLst;
We use cookies
byte cylLst;
bool sync;
🍪
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
adjustcounterCyl;
int this in Cookie Settings or learn more by reading our cookie policy.
int counterIdent;
unsigned long CurrTime;
unsigned long StrobeTime;
Lewati ke konten utama
void setup ()
{
//define IO pins
pinModeFast (cyl14, OUTPUT);
pinModeFast (cyl52, OUTPUT);
pinModeFast (cyl36, OUTPUT);
void loop ()
{
unsigned long msec = millis ();
if (HIGH == ident)
counterCyl = 0;
}
if (HIGH == cyl) {
counterCyl++;
// engine stopped
if (msec - StrobeTime > 500) {
sync = 0;
}
if (sync) {
digitalWriteFast (cyl14, counterCyl == 1 || counterCyl == 4);
digitalWriteFast (cyl52, counterCyl == 5 || counterCyl == 2);
digitalWriteFast (cyl36, counterCyl == 3 || counterCyl == 6);
}
}
counterIdent = 0;
}`
We use
engine, cookies
messing 🍪
is used to detect engine stalling, as stalling could result in a brief opposide direction rotation of the
up the counting. This resets counterIdent to zero, resulting in sync=false, thus the
sensor needs again a full revolution to synchnorize ident and cyl before enabling outputs.
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
adjust this in Cookie Settings or learn more by reading our cookie policy.
Coding_Badly Des '22 post #25
Koepel:
Lewati
or evenke konten utama
uin8_t
...which is risky / problematic because of the promotion rules. Please don't suggest that again to
folks new to C programming.
GolamMostafa:
That means that the written communication can violate C++ Language's established
conventions?
When "we" say code we often forget the compiler only gets what the pre-processor reguritates.
Capitalization Rules in Writing (tutorialspoint.com)
Capitalize acronyms
Acronyms are the initials of the first letter of multiple words. It is mandatory to capitalize the whole
acronym while writing it in any sentence.
StrobeTime = millis();
Serial.println(CurrTime-StrobeTime);
neubauerp
We use cookies 🍪
No, millis is only used to detect if the engine has stalled.
Des '22 post #31
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
adjust this in Cookie Settings or learn more by reading our cookie policy.
RayLivingston:
Timing jitter would be massive,
Back to top
Brand Guidelines
Distributors
Careers
We use cookies 🍪
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can
adjust this in Cookie Settings or learn more by reading our cookie policy.