0

In the last condition in else if (see code marked with ***), I am not getting my screen with the laser/specklke fail, if both failflag 1 and failgflag 5 is there, however it shows me laser fail instead. Please help to solve my error.

#include <UTFT.h>
#include <avr/pgmspace.h>
#include <SPI.h>
#include <avr/wdt.h>
extern uint8_t Retro8x16[];
extern unsigned int virtek3[]; //display pic
UTFT myDisplay(ST7735, 11, 13, 3, 2, 4);
int buttonhold = 0;
int state = HIGH;
int tim = 0;
int tim1 = 0;
int tim2 = 0;
int button = 1;
float volt = 0;
float volt1 = 0;
float volt2 = 0;
float volt3 = 0;
float volt4 = 0;
float volt5 = 0;
float volt6 = 0;
float volt7 = 0;
float volt8 = 0;
float volt9 = 0;
float volt10 = 0;
float volt11 = 0;
float volt12 = 0;
float volt13 = 0;
float volt14 = 0;
float volt15 = 0;
float volt16 = 0;
float volt17 = 0;
float volt18 = 0;
float volt19 = 0;
float volt20 = 0;
float volt21 = 0;
float vcc = 0.0;
float temp = 0.0;
float r1 = 10000.0;
float r2 = 2200.0;
int failflag = 0;
//void(* resetFunc) (void) = 0;
void setup()
{
 pinMode(18, INPUT); //For NEXT button
 pinMode(20, OUTPUT); //For Relay power
 pinMode(21, OUTPUT); //For Relay power
 pinMode(14, INPUT);
 Serial.begin(9600);
 myDisplay.InitLCD();
 myDisplay.clrScr();
 myDisplay.setFont(Retro8x16);
 digitalWrite(20, HIGH);
 digitalWrite(20, HIGH);
 digitalWrite(21, LOW);
 attachInterrupt(digitalPinToInterrupt(18), next, LOW);
 myDisplay.fillScr(255, 255, 255);
 myDisplay.drawBitmap(20, 1, 126, 126, virtek3, 1);
 while (1) {
 delay(100);
 if (state == LOW) {
 break;
 }
 } 
}
void next() {
 state = LOW;
 Serial.println(buttonhold);
 buttonhold++;
 if (buttonhold > 500) {
 //digitalWrite(21, LOW);
 resetFunc(); 
 }
 }
void loop()
{
 digitalWrite(20, LOW);
 digitalWrite(21, LOW); //Relay is ON.
 myDisplay.clrScr();
 state = HIGH;
 buttonhold = 0;
 while (1) {
 failflag = 0;
 float voltage_value = 0;
 myDisplay.setColor(0, 255, 255);
 myDisplay.print("1.Voltage Test", 35, 0);
 myDisplay.setColor(255, 255, 255);
 myDisplay.print("(V)", 0, 20);
 myDisplay.print("LASER",50,20);
 myDisplay.print("SPECKLE",100,20);
delay(1000);
 //FOR ANALOG A4 PIN
 //A4 pin connected to TP2 of speckle
 float volt_b = 0;
 float volt_c = 0;
 float volt_d = 0;
 for (int i = 0; i < 1000; i++) {
 volt_b =(volt_b + (4.99 / 1024 * analogRead(A4))); 
 }
 volt_b = volt_b / 1000;
 volt1 = volt_b / (r2 / (r1 + r2));
 myDisplay.setColor(255, 255, 255);
 myDisplay.print("TP2 :", 0, 38); 
 myDisplay.setColor(0, 255, 255);
 myDisplay.print("NA", 60, 37);
 Serial.println(volt1); //ANALOG A4
 if (volt1 >= 5 && volt1 <= 7.30) {
 myDisplay.setColor(0, 255, 0);
 myDisplay.printNumF(volt1, 2, 120, 38);
 //myDisplay.print(" ", 130, 20);
 } else { 
 myDisplay.setColor(0, 0, 255);
 myDisplay.printNumF(volt1, 2, 120, 38);
 //myDisplay.print(" ", 150, 38);
 failflag = 1;
 }
 delay(2000);
 digitalWrite(20, HIGH);
 digitalWrite(21, LOW);
 delay(2000);
 //FOR ANALOG A4 PIN
 //A4 pin connected to TP2/L of SPECKLE
 for (int i = 0; i < 1000; i++) {
 volt_b =(volt_b + (4.99 / 1024 * analogRead(A4))); 
 }
 volt_b = volt_b / 1000;
 volt2 = volt_b / (r2 / (r1 + r2));
 myDisplay.setColor(255, 255, 255);
 myDisplay.print("TP2/L :", 0, 56);
 myDisplay.setColor(0, 255, 255);
 myDisplay.print("NA", 60, 56);
 Serial.println(volt2); //ANALOG A4
 if (volt2 >= 5 && volt2 <= 0.05) {
 myDisplay.setColor(0, 255, 0);
 myDisplay.printNumF(volt2, 2, 120, 56);
 //myDisplay.print(" ", 130, 20);
 } else { 
 myDisplay.setColor(0, 0, 255);
 myDisplay.printNumF(volt2, 2, 120, 56);
 //myDisplay.print(" ", 150, 56);
 failflag = 1;
 }
 //FOR ANALOG A1 PIN
 //A1 pin connected to J1_1 of SENSOR
 volt = analogRead(A1);
 volt3 =(volt * 4.8) /1024;
 myDisplay.setColor(255, 255, 255);
 myDisplay.print("J1_1 :", 0, 74);
 Serial.println(volt3); //ANALOG A1
 if (volt3 >= 0 && volt3 <= 5.25) {
 myDisplay.setColor(0, 255, 0);
 myDisplay.printNumF(volt3, 2, 60, 74);
 //myDisplay.print(" ", 130, 20);
 } else { 
 myDisplay.setColor(0, 0, 255);
 myDisplay.printNumF(volt3, 2, 60, 74);
 //myDisplay.print(" ", 150, 74);
 failflag = 5;
 }
 myDisplay.setColor(0, 255, 255);
 myDisplay.print("NA", 125, 74);
 //FOR ANALOG A0 PIN
 //A0 pin connected to J2_2 of SENSOR
 for (int i = 0; i < 1000; i++) {
 volt_b =(volt_b + (4.99 / 1024 * analogRead(A0))); 
 }
 volt_b = volt_b / 1000;
 volt4 = volt_b / (r2 / (r1 + r2));
 //volt4 = volt_c - 2.7;
 myDisplay.setColor(255, 255, 255);
 myDisplay.print("J2_2 :", 0, 92);
 Serial.println(volt4); //ANALOG A0
 if (volt4 >= 5 && volt4 <= 8.82) {
 myDisplay.setColor(0, 255, 0);
 myDisplay.printNumF(volt4, 2, 60, 92);
 //myDisplay.print(" ", 130, 20);
 } else {
 myDisplay.setColor(0, 0, 255);
 myDisplay.printNumF(volt4, 2, 60, 92);
 //myDisplay.print(" ", 150, 92);
 failflag = 5;
 }
 myDisplay.setColor(0, 255, 255);
 myDisplay.print("NA", 125, 93);
 digitalWrite(20, LOW);
 digitalWrite(20, LOW);
 delay(2000);
 if (failflag == 5) {
 myDisplay.setColor(0, 0, 255);
 myDisplay.print("LASER FAIL", 35, 108);
 delay(4000);
 state = 0;
 }
 else if (failflag == 1) {
 myDisplay.setColor(0, 0, 255);
 myDisplay.print("SPECKLE FAIL", 35, 108);
 delay(40000);
 state = 0;
 }
 // *** Here the condition not working START
 else if ((failflag == 5) && (failflag == 1)) {
 myDisplay.setColor(0, 0, 255);
 myDisplay.print("LASER/SPECKLE FAIL", 15, 108);
 delay(40000);
 state = 0;
 }
 // *** Here the condition not working END
Codebreaker007
1,3311 gold badge7 silver badges14 bronze badges
asked Apr 3, 2020 at 20:58
1
  • 1
    what is the program supposed to do? .... what are you expecting to happen? .... what actually happens? .... do you get any errors? Commented Apr 3, 2020 at 21:10

2 Answers 2

5
(failflag == 5) && (failflag == 1))

failflag can't be both 5 and 1 at the same time.

answered Apr 3, 2020 at 21:20
1

As you ask for two different assignments for the same var its never gone to work this way. Two quick options:
Do binary addidition

 failflag = 0;
 if(errorA) failflag += 1;
 if(errorB) failflag += 2;
 if(errorC) failflag += 4;
 if(errorD) failflag += 8;
//then you can decode combined errors as follows
else if (failflag == 13) {
 Serial.println("Errors: A, C, D"); // 1 + 4 + 8 =13
}

The other quick one is defining failflag as bool array

bool failflag [5] = {00000}; // 5 elements
 if(errorA) failflag [0]= 1;
 if(errorB) failflag [1]= 1;
 if(errorC) failflag [2]= 1;
 if(errorD) failflag [3]= 1;
//then you can decode combined errors as follows
else if ( failflag [0] == 1 && failflag [2]== 1 && failflag [3]== 1) {
 Serial.println("Errors: A, C, D"); // 1 + 4 + 8 =13
}

as we have failflag[i] as a bool we can condense the code to

else if ( failflag [0] && failflag [2] && failflag [3]) {
 Serial.println("Errors: A, C, D"); // 1 + 4 + 8 =13
}

Whatever suits your needs. As I said just 2 quick ones
Edit Cleaning up you question other things to change:
Get rid of the delay() (except in setup to initialize your hardware and in loop during debuging.

The 21 float vars could be stores in a float array

 float myFloatVars [22]

and write the values to the position

 myFloatVars [11] = myMeasureFunction();

you can do this even in a timed for loop doing a recurring FIFO function (kind of circle buffer), the first saved value is dumped if the array is full to put the next one in. So you are able to produce a "gliding" average, which is what you want

answered Apr 4, 2020 at 9:37
6
  • bool data type is true/false, so the test is if ( failflag [0]) or if ( !failflag [0]) .... if ( failflag [0]== 1) equates to if ( true==true), which is redundant Commented Apr 4, 2020 at 17:09
  • Your right copy & paste error. changed it to true so that the OP sees whats going on - the redundancy helps beginners to understand. Thanks for the eagle eye Commented Apr 4, 2020 at 17:15
  • note: null termination is used for strings, not for numerical arrays Commented Apr 4, 2020 at 17:21
  • this is for OP's benefit .... if(errorA) failflag [0]= 1; can be replaced by failflag[0] = errorA, but only if the failflag[0] is not set by multiple sources Commented Apr 4, 2020 at 17:28
  • Thank you so much . I use bool array and it works. Really appreciate you legend ! Commented Apr 6, 2020 at 20:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.