I have the following snippet of code for activating a blinker by using a virtual button displayed on a nextion display. I get the value 0 or 1 which is than written to br3s. HIGH means button has 1 value. If the vehicle has centered steering angle is zero. Now I activate blinke by pressing the button. If I steer at least by 10 degrees and return to center the blinker is switched off and the button state is overwritten to 0. This works fine for the first time but after I need to turn the steering angle above 10 degrees for activating the blinker. If not blinker button gets immediately reset to 0 again. What do I need to change?
//routine for automated right blinker set and reset
if (br3s == HIGH && br3son == false){
br3son = true;
hazardon = false;
digitalWrite(rightblinkerPin, HIGH); //switch on right blinker and
digitalWrite(leftblinkerPin, LOW); //left blinker off
}
if(br3son == true && delta_deg > 10 && turned_right == false)
{
turned_right = true;
}
if(delta_deg <= 10 && turned_right == true)
{
turned_right == false;
turned_from_right_to_center = true;
}
if (br3son == true && turned_from_right_to_center == true)
{
turned_from_right_to_center = false;
digitalWrite(rightblinkerPin, LOW);//switch off right blinker
bt8_1.setValue(0);
br3son = false;
}
The first thing to change is to post the complete sketch.
In 90% of all cases the bug that causes a different code-behaviour than wanted is outside the lines you think the bug is.
You are using variables in the posted code-snippet that get assigned values outside the presented code-snippet. delta_deg
It might be that other variables get values assigned outside the presented code-snippet.
How should anybody analyse your code-logic if you present only a part of your code?
Your code uses a lot of boolean flag-variables which where set true / false to make it work.
There is a different approach for programming such functionalities.
This approach is called state-machine.
state1: steering is centered checking for is blinker switched on?
if yes change to state2
state2: check if steering-angle is above 10 degrees?
if yes change to state3
state3: check if steering-angle is below 10 degrees?
if yes switch blinker off and change to state1
the state-machine-approach minimises the flag-variables to the nescessary minimum.
because in each state only that part of the code is executed that is relevant for this state
I moved your topic to an appropriate forum category @marcel00.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
If you wrote this code in stages, getting each stage to work then combining all the stages one at a time, you should know what you did to make this fault occur.
Now if you didn't and have written the code as one lump, then STOP.
Forget you main code for the while and write code just to do what you want to happen.
That is code that JUST reads the steering angle an the button and produces the turn indicator responses.
That way you can be assured that the bug is in this bit of code and not caused by the rest that we cannot see or want to read.
So start coding for thee steering angle, buttons and direction indicators.
Also a simple schematic of your hardware and link to data/spec of the turn angle sensor.
I haven't found any value-assigning to this variable in the code that you have posted so far.
So if this variable
delta_deg
is nowhere assigned any value
how can you expect that your code works?
I am very sure that you assign variable
delta_deg
values anywhere else than in the lines of code that you have posted.
So don't you think it makes sense to post your complete sketch?
What will happen if you post your complete sketch?
Does this violate a non-disclosure-agreement?
Do you get a heavy headache from it?
Will your bankaccount become empty if you post your complete sketch?
Will you run out of datatransmission-volume for your internetconnection?
Well to state the obvious
in your code in line 3489 you have to assign variable
delta_deg
a negative value then everything will work.
(no just kidding)
The following code for function blinker works for my application. The step markers are set to false if the blinker for left or right is switched off in the last step. For example for left turn:
The angle sensor has got zero at the center and turns negative to the left and positive to the right up to +-25°. It is a AMS5600 above a oriented permanent magnet situated above the steering axis.
void blinker()
{
leftb.getValue(&gpio_3);
rightb.getValue(&gpio_1);
bt11_1.getValue(&bl3s);
bt8_1.getValue(&br3s);
if ( gpio_3 == LOW && blinkerlefton == false) {
blinkerlefton = true;
hazardon = false;
digitalWrite(leftblinkerPin, HIGH);
digitalWrite(rightblinkerPin, LOW);
}
if (gpio_3 == HIGH && blinkerlefton == true) {
blinkerlefton = false;
digitalWrite(leftblinkerPin, LOW);
}
if (( gpio_1 == LOW ) && blinkerrighton == false) {
blinkerrighton = true;
hazardon = false;
digitalWrite(rightblinkerPin, HIGH);
digitalWrite(leftblinkerPin, LOW);
}
if ((gpio_1 == HIGH ) && blinkerrighton == true) {
blinkerrighton = false;
digitalWrite(rightblinkerPin, LOW);
}
//blinker activated by screen buttons
if(gpio_3 == HIGH && gpio_1 == HIGH){
//routine for automated left blinker set and reset
if (bl3s == HIGH && bl3son == false){
bl3son = true;
hazardon = false;
digitalWrite(leftblinkerPin, HIGH);//switch on left blinker and
digitalWrite(rightblinkerPin, LOW);// right blinker off
}
if(bl3son == true && delta_deg < -10 && turned_left == false)//if steering turned to left more than 10°
{
turned_left = true;
}
if(bl3son== true && delta_deg >= -10 && turned_left == true)//if steering returned back to center
{
turned_from_left_to_center = true;
}
if (bl3son == true && turned_from_left_to_center == true)
{
digitalWrite(leftblinkerPin, LOW);// switch off left blinker
bt11_1.setValue(0);
bl3son = false;
turned_left = false;
turned_from_left_to_center = false;
}
//routine for automated right blinker set and reset
if (br3s == HIGH && br3son == false){
br3son = true;
hazardon = false;
digitalWrite(rightblinkerPin, HIGH);//switch on left blinker and
digitalWrite(leftblinkerPin, LOW);// right blinker off
}
if(br3son == true && delta_deg > 10 && turned_right == false)//if steering turned to left more than 10°
{
turned_right = true;
}
if(br3son== true && delta_deg <= 10 && turned_right == true)//if steering returned back to center
{
turned_from_right_to_center = true;
}
if (br3son == true && turned_from_right_to_center == true)
{
digitalWrite(rightblinkerPin, LOW);// switch off left blinker
bt8_1.setValue(0);
br3son = false;
turned_right = false;
turned_from_right_to_center = false;
}
}
}
Finally I changed the function blinker for being able to turn off the blinker if a blinker button was pressed on the display by mistake.
So let me describe the total function:
By pressing a dual state button which is available by Nextion software I will be able to switch on and off a blinker. If I switch on a blinker and I turn the steering axis to a certain degree the MCU realizes it and waits with turning off the blinker until the steering axis returns to center thus recognizes it by a steering angle entering below the prior mentioned threshold angle.
In addition to the dual state buttons I can use a mechanical switch as commonly used at a motorbike. The signals are sent to the Nextion display via gpio extension board and Nextion transfers the signals towards a MEGA 2560.
Here is the code
void blinker()
{
leftb.getValue(&gpio_3);
rightb.getValue(&gpio_1);
bt11_1.getValue(&bl3s);
bt8_1.getValue(&br3s);
if ( gpio_3 == LOW && blinkerlefton == false) {
blinkerlefton = true;
hazardon = false;
digitalWrite(leftblinkerPin, HIGH);
digitalWrite(rightblinkerPin, LOW);
}
if (gpio_3 == HIGH && blinkerlefton == true) {
blinkerlefton = false;
digitalWrite(leftblinkerPin, LOW);
}
if (( gpio_1 == LOW ) && blinkerrighton == false) {
blinkerrighton = true;
hazardon = false;
digitalWrite(rightblinkerPin, HIGH);
digitalWrite(leftblinkerPin, LOW);
}
if ((gpio_1 == HIGH ) && blinkerrighton == true) {
blinkerrighton = false;
digitalWrite(rightblinkerPin, LOW);
}
//blinker activated by screen buttons
if(gpio_3 == HIGH && gpio_1 == HIGH){
//routine for automated left blinker set and reset
if (bl3s == HIGH && br3s == LOW && bl3son == false){
bl3son = true;
hazardon = false;
digitalWrite(leftblinkerPin, HIGH);//switch on left blinker and
digitalWrite(rightblinkerPin, LOW);// right blinker off
}
if(br3s == LOW && bl3son == true && delta_deg < -10 && turned_left == false)//if steering turned to left more than 10°
{
turned_left = true;
}
if(br3s == LOW && bl3son== true && delta_deg >= -10 && turned_left == true)//if steering returned back to center
{
turned_from_left_to_center = true;
}
if (((bl3s == LOW || br3s == HIGH) && bl3son == true)||( bl3son == true && turned_from_left_to_center == true))
{
digitalWrite(leftblinkerPin, LOW);// switch off left blinker
bt11_1.setValue(0);
bl3son = false;
turned_left = false;
turned_from_left_to_center = false;
}
//routine for automated right blinker set and reset
if (br3s == HIGH && bl3s == LOW && br3son == false){
br3son = true;
hazardon = false;
digitalWrite(rightblinkerPin, HIGH);//switch on left blinker and
digitalWrite(leftblinkerPin, LOW);// right blinker off
}
if(bl3s == LOW && br3son == true && delta_deg > 10 && turned_right == false)//if steering turned to left more than 10°
{
turned_right = true;
}
if(bl3s == LOW && br3son== true && delta_deg <= 10 && turned_right == true)//if steering returned back to center
{
turned_from_right_to_center = true;
}
if (((br3s == LOW || bl3s == HIGH) && br3son == true)||( br3son == true && turned_from_right_to_center == true))
{
digitalWrite(rightblinkerPin, LOW);// switch off left blinker
bt8_1.setValue(0);
br3son = false;
turned_right = false;
turned_from_right_to_center = false;
}
}
}