Arduino can't get my boolean to work - c++

I cant get my boolean to work I don't know what I'm doing wrong with it. Could anyone take a look at the code and give me a hint on what is wrong with it? I have tested different ways to write it but without success. The only time the boolean worked was when I put the code under void loop. But I can't use it there.
#include <RemoteReceiver.h>
boolean statusLed1 = false;
void setup() {
Serial.begin(115200);
// Initialize receiver on interrupt 0 (= digital pin 2), calls the callback "showCode"
// after 3 identical codes have been received in a row. (thus, keep the button pressed
// for a moment)
//
// See the interrupt-parameter of attachInterrupt for possible values (and pins)
// to connect the receiver.
RemoteReceiver::init(0, 3, showCode);
}
void loop() {
}
// Callback function is called only when a valid code is received.
void showCode(unsigned long receivedCode, unsigned int period) {
// Note: interrupts are disabled. You can re-enable them if needed.
// Print the received code.
Serial.print("Code: ");
Serial.print(receivedCode);
Serial.print(", period duration: ");
Serial.print(period);
Serial.println("us.");
if (receivedCode == 353805)
{
statusLed1 = true;
}
if (receivedCode == 352829)
{
statusLed1 = false;
}
if (statusLed1 = true) {
Serial.print("on");
}
if (statusLed1 = false){
Serial.print("off");
}
}

if (statusLed1 = true) {
Oldest gotcha in the book. = is assignment, == is equality comparison.
Also, don't compare against a boolean like this regardless.
if (statusLed1) {

change
if (statusLed1 = true) {
Serial.print("on");
}
if (statusLed1 = false){
Serial.print("off");
}
}
to
if (statusLed1 == true) {
Serial.print("on");
}
if (statusLed1 == false){
Serial.print("off");
}
}
or
if (statusLed1) {
Serial.print("on");
}
if (!statusLed1){
Serial.print("off");
}
}

Related

Do I need to add some kind of check before continuing when hopping to other void

I'm new(-ish) to C++, and I'm trying to learn something new every day. Today I'm trying to figure this out.
Do I have to check if Valmis == 1 so it won't continue/return before Realtest() is complete?
This is just an example.
int valmis = 0;
void test::Main()
{
//just some basic stuff
do
{
if (!strcmp())
{
//here too
float testing = 0;
printf("Test? 1=do it Else=Nothing\n");
scanf(" %f", &testing);
if (testing == 1)
{
Realtest();
//Realtest needs to be completed before continuing
if (valmis == 1) //Do I need this or does it continue after RealTest() is complete without this?
return (somethingmate = true);
}
else
{
return (somethingmate = true);
}
}
} while();
return (somethingmate = false);
}
void test::Realtest()
{
//doing something here that I need to do before continuing in main/letting somethingmate to become true
valmis = 1; //do i need this?
}

Global variable doesn't update prior to next loop

I'm trying to build a tachometer in C++ for my ESP32. When I uncomment Serial.printf("outside rev: %d \n", rev); outside of the conditional it works, but when I comment it I get values that are orders of magnitude greater than they should be (700 revolutions without, vs 7 revolutions with). My best guess is that the print statement is slowing the loop() down just enough for incrementRevolutions() to toggle the global variable passedMagnet from true to false before the next loop. That would make sense, since a delay in updating passedMagnet would allow newRevCount++; to be triggered multiple times. But this is obviously something I can't debug with either print statements or step-through debugging given the time-sensitive nature of the race condition.
bool passedMagnet = true;
int incrementRevolutions(int runningRevCount, bool passingMagnet)
{
// Serial.printf("passedMagnet: %d , passingMagnet %d , runningRevCount: %d \n", passedMagnet, passingMagnet, runningRevCount);
int newRevCount = runningRevCount;
if (passedMagnet && passingMagnet)
{ //Started a new pass of the magnet
passedMagnet = false;
newRevCount++;
}
else if (!passedMagnet && !passingMagnet)
{ //The new pass of the magnet is complete
passedMagnet = true;
}
return newRevCount;
}
unsigned long elapsedTime = 0;
unsigned long intervalTime = 0;
int rev = 0;
void loop()
{
intervalTime = millis() - elapsedTime;
rev = incrementRevolutions(rev, digitalRead(digitalPin));
// Serial.printf("outside rev: %d \n", rev);
if (intervalTime > 1000)
{
Serial.printf("rev: %d \n", rev);
rev = 0;
elapsedTime = millis();
}
}
Is this a known gotcha with Arduino or C++ programming? What should I do to fix it?
I think the test is to blame. I had to rename and move things a bit to visualize the logic, sorry about that.
bool magStateOld = false; // initialize to digitalRead(digitalPin) in setup()
int incrementRevolutions(int runningRevCount, bool magState)
{
int newRevCount = runningRevCount;
// detect positive edge.
if (magState && !magStateOld) // <- was eq. to if (magState && magStateOld)
// the large counts came from here.
{
newRevCount++;
}
magStateOld = magState; // record last state unconditionally
return newRevCount;
}
You could also write it as...
int incrementRevolutions(int n, bool magState)
{
n += (magState && !magStateOld);
magStateOld = magState;
return n;
}
But the most economical (and fastest) way of doing what you want would be:
bool magStateOld;
inline bool positiveEdge(bool state, bool& oldState)
{
bool result = (state && !oldState);
oldState = state;
return result;
}
void setup()
{
// ...
magStateOld = digitalRead(digitalPin);
}
void loop()
{
// ...
rev += (int)positiveEdge(digitalRead(digitalPin), magStateOld);
// ...
}
It's reusable, and saves both stack space and unnecessary assignments.
If you cannot get clean transitions from your sensor (noise on positive and negative edges, you'll need to debounce the signal a bit, using a timer.
Example:
constexpr byte debounce_delay = 50; // ms, you may want to play with
// this value, smaller is better.
// but must be high enough to
// avoid issues on expected
// RPM range.
// 50 ms is on the high side.
byte debounce_timestamp; // byte is large enough for delays
// up to 255ms.
// ...
void loop()
{
// ...
byte now = (byte)millis();
if (now - debounce_timestamp >= debounce_delay)
{
debounce_timestamp = now;
rev += (int)positiveEdge(digitalRead(digitalPin), magStateOld);
}
// ...
}

Calling function once

if (GetKeyState(VK_DOWN) & 0x80)
{
func();
}
It calls func() like 4 times when I press key
I want it to call only once when I press key
EDIT:
SHORT keyState;
SHORT keyState2;
SHORT keyState3;
static bool toogle1 = false;
static bool toogle2 = false;
static bool toogle3 = false;
if (keyState = GetAsyncKeyState(VK_DOWN) && !toogle1)
{
toogle1 = true;
}
else
toogle1 = !toogle1;
if (keyState2 = GetAsyncKeyState(VK_NUMPAD0) && !toogle2)
{
toogle2 = true;
}
else
toogle2 = !toogle2;
if (keyState3 = GetAsyncKeyState(VK_NUMPAD1) && !toogle3)
{
toogle3 = true;
}
else
toogle3 = !toogle3;
Here is how I did it, will it work?
static bool once = false;
if (GetKeyState(VK_DOWN) & 0x80)
{
if (!once)
{ once = true; func(); }
}
I guess you run this in a loop. Your idea with toggling a flag when called is not bad, but since you toggle it back in the else case, you call func() half as often as before (every 2nd time).
When you want to call it again (I think you want, according to your code), when the key is pressed again, but not spam the function call, you can use a variable to store which key was pressed last and only call func(), when it was another key (you can also add a "no key pressed" state.
If you really just want to call it once, just remove your else statements.

Stop arduino loop when a condition is active

I need my code to stop in the loop, i have tried to put in a break but the method sendToGCM() continues. I only want the method to be executed once, the stop while the condition
void loop()
{
// Other code here giving temp a value
if (temp > 22)
{
status = false;
value["status"] = status;
while (temp > 22)
{
sendToGCM(); // Sends push notification
break;
}
}
else
{
status = true;
value["status"] = status;
}
}
So if I understood you correctly, if the temperature goes 22 degrees you want to send a message, but only the first time. If you break the loop, you still enter it if you the loop() function is executed again.
In order to achieve what you want to do, you code will need to look something like this
boolean message_sent;
void loop() {
...
if(temperature > 22 && !message_sent) {
sendToGCM();
message_sent = true;
}
}
If you want to send a message every time the temperature rises over 22 degrees you would need something like this
boolean message_sent;
boolean was_under_22;
void setup() {
...
was_under_22 = function_that_checks_if_temp_is_under_22();
...
}
void loop() {
...
if(temperature > 22 && was_under_22) {
if(!message_sent) {
sendToGCM();
message_sent = true;
was_under_22 = false;
}
} else {
was_under_22 = true;
message_sent = false;
}
}
EDIT: slightly adapted the code in response to Patrick Trentin's comment. The code assumes you only want to capture if the temperature rises above 22 degrees and that if the Arduino starts with over 22 degrees then no message is sent.
Your problem is that you are setting temp, then entering the loop that checks that value. A simple solution would be to update the temp value inside the while loop, to give the application a chance to break out of the while loop.
Example:
void loop()
{
// Other code here giving temp a value
if (temp > 22)
{
status = false;
value["status"] = status;
while (temp > 22)
{
sendToGCM(); // Sends push notification
//Additional code to set the value of temp, allowing the value to
//be greater than 22.
}
}
else
{
status = true;
value["status"] = status;
}
}
Please note that the above example is intended to continuously send the push notification while the temp value is over 22. If that's not the intention, just remove the sendToGCM() from the while loop. You will still only send it if the temp is greater than 22, since you have the if check.

Execute a piece of code in a function from the second invocation onwards

If I desire to run a piece of code in a function, only from the second invocation of the function onwards,
Questions:
Is there something wrong to do that?
How can I possibly achieve this ? Is using a static variable to do this a good idea ?
There's two answers to this question, depending on whether you have to deal with multi-threaded serialization or not.
No threading:
void doSomething() {
static bool firstTime = true;
if (firstTime) {
// do code specific to first pass
firstTime = false;
} else {
// do code specific to 2nd+ pass
}
// do any code that is common
}
With threading:
I'll write the generic boilerplate, but this code is system specific (requiring some variant of an atomic compareAndSet).
void doSomethingThreadSafe() {
static volatile atomic<int> passState = 0;
do {
if ( passState == 2 ) {
//perform pass 2+ code
break;
} else
if ( passState.compareAndSet(0,1) ) { // if passState==0 set passState=1 return true else return false
//perform pass 1 initialization code
passState = 2;
break;
} else {
//loser in setup collision, delay (wait for init code to finish) then retry
sleep(1);
}
} while(1);
//perform code common to all passes
}
Multi-threading will be a problem. To prevent this, if required, you'll probably need something like a mutex.
Like this:
void someFunction()
{
static bool firstRun = true;
if (!firstRun)
{
// code to execute from the second time onwards
}
else
{
firstRun = false;
}
// other code
}
Add a global counter.
eg:-
static int counter = 0;
public void testFunc(){
if(counter==1){
........
<Execute the functionality>
........
}
counter++;
}