Notifications
Clear all

INA 260, trouble with an if statement

57 Posts
9 Users
59 Reactions
4,846 Views
(@quince)
Member
Joined: 3 years ago
Posts: 40
Topic starter  

Hello all, I am having trouble modifying an Adafruit ina260 example program. I am  using a NANO and Adafruit  ina260 current sensor,  to operate  a 5VDC normally closed relay that in turn will  switch off current to a motor in case of overload. The motor armature can be stalled in some conditions where it must operate leading to a massive overload and motor destruction.   The goal is that the components once assembled can  act as an very fast acting, auto reset electronic fuse with a preset cool-down period when amp tolerance presets are exceeded. It seem simple project to work on "if" statements and the Adafruit ina260 performs perfect. It can be read on the serial monitor. But after several hours I cant get to the bottom of my code issues. I am very inexperienced, this is the first time I have tried to combine an example with modifications to the code to achieve the desired result.  I hope the error is simple  but I can't find it. I keep getting the error message on the second if statement, any help would be appreciated, Thanks in advance, Quince

 

#include <Wire.h>

#include <Adafruit_INA260.h>

int mypinout = 9;
int mypinin = 4;
float val = 0;

Adafruit_INA260 ina260 = Adafruit_INA260();

void setup() {
Serial.begin(115200);
// Wait until serial port is opened
while (!Serial) {
delay(10);
pinMode(mypinout, OUTPUT);
pinMode(mypinin, INPUT);

}
Serial.println("Adafruit INA260 Test");

if (!ina260.begin()) {
Serial.println("Couldn't find INA260 chip");
while (1);
}

Serial.println("Found INA260 chip");
}

void loop() {
val = analogRead(mypinin);
digitalWrite(mypinout, val);

Serial.print("Current: ");
Serial.print(ina260.readCurrent());
Serial.println(" mA");

Serial.print("Bus Voltage: ");
Serial.print(ina260.readBusVoltage());
Serial.println(" mV");

Serial.print("Power: ");
Serial.print(ina260.readPower());
Serial.println(" mW");

if ()) {
(val,> 2000,) digitalWrite, mypinout, HIGH);
};
delay(25000);
if ()) {
(val, < 2000) digitalWrite, mypinout, LOW);
}

Serial.println();
delay(100);
}

 

 

 

 

 

 

 

 

 

 

 

 

C In function 'void loop()':

ina260_modifed:47:7: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:47:8: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:51:7: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:51:8: error: expected primary-expression before ')' token

if ()) {

^

exit status 1

expected primary-expression before ')' token

 

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

 

 

 

 

 

 

 

 

 

C:\Users\James\Documents\Arduino\ina260_modifed\ina260_modifed.ino: In function 'void loop()':

ina260_modifed:47:7: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:47:8: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:51:7: error: expected primary-expression before ')' token

if ()) {

^

ina260_modifed:51:8: error: expected primary-expression before ')' token

if ()) {

^

exit status 1

expected primary-expression before ')' token

 

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

 


   
Quote
(@yurkshirelad)
Member
Joined: 4 years ago
Posts: 495
 

The IF statement isn't checking anything, it needs a condition to check:

if ()) {

The condition can't be empty, e.g.

if (myFlag == true)) {

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@quince

Posted by: @quince

[snip]

It seem simple project to work on "if" statements and the Adafruit ina260 performs perfect. It can be read on the serial monitor. But after several hours I cant get to the bottom of my code issues. I am very inexperienced, this is the first time I have tried to combine an example with modifications to the code to achieve the desired result.  I hope the error is simple  but I can't find it. I keep getting the error message on the second if statement, any help would be appreciated, Thanks in advance, Quince

[snip]

if ()) {
(val,> 2000,) digitalWrite, mypinout, HIGH);
};

Your first one looked ok, but as @yurkshirelad explained, your conditional statement
had the incorrect syntax - This is how it should look like:

if (condition) {
  //statement(s)
 }

...where 'condition' evaluates to either 'true' or 'false'

Cheers.


   
ron bentley reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@yurkshirelad 

Posted by: @yurkshirelad

The condition can't be empty, e.g.

if (myFlag == true)) {

You might still have time to edit out that extra bracket?

Cheers


   
ron bentley reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 3 years ago
Posts: 554
 

Hi @quince, As @yurkshirelad has already commented, I'll just clarify the comment..

change your if statement.

if ()) {
(val,> 2000,) digitalWrite, mypinout, HIGH);
};

to this:

if (val > 2000){

digitalWrite(mypinout, HIGH);

}

and similarly, change your other "if" statement as well..

regards,

LouisR

LouisR


   
ron bentley and Ron reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@inst-tech 

Just a tip when posting code, for you and other members viewing... When in reply mode, firstly, in your Arduino IDE, select the code that you want to copy, then right click on your highlighted code selection and choose "Copy as HTML", and then paste that HTML code into the source code window, which you can find by clicking on the following button on your reply screen as shown below:

image

Cheers.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7660
 

Remove the extra right paren, you have in multiple places if()) it should be if().

Try the auto format menu option. Tru using arduino2, I think it will catch this or better still platformIO

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ron bentley reacted
ReplyQuote
(@codeslinger)
Member
Joined: 5 years ago
Posts: 30
 

@yurkshirelad The empty condition is fine.  The problem is that there are one left paren and two right parens.  The second should not be there.  That is exactly what the compiler is saying.


   
ron bentley reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7660
 

@codeslinger BINGO

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 4 years ago
Posts: 495
 
Posted by: @codeslinger

@yurkshirelad The empty condition is fine.  The problem is that there are one left paren and two right parens.  The second should not be there.  That is exactly what the compiler is saying.

Ah - I wasn't wearing my reading glasses. 🤣 


   
ReplyQuote
(@quince)
Member
Joined: 3 years ago
Posts: 40
Topic starter  

Hello All, Once again the problem is solved. Thank you, Quince


   
ReplyQuote
(@quince)
Member
Joined: 3 years ago
Posts: 40
Topic starter  

@inst-tech Thanks Louis, Notation seem to be my issue. You made the solution very clear. Quince


   
Inst-Tech reacted
ReplyQuote
(@codeslinger)
Member
Joined: 5 years ago
Posts: 30
 

@frogandtoad NEVER compare a value or experssion to true or false.  If X is a value or expression then the proper form is 

if( X ) // check if X is true

or

if( !X ) to check if X is false.

This is expecially problematic for comparisons to true.  C/C++ defines any value not equal to zero as true and any equal to zero as false.  The constants true and false are defined as zero and some non-zero value.  For instance if, true is defined as 1 but X evaluates to 2, then X==true is false and would be false for any X not equal to 1.  This is counter to the above definition of true and false.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7660
 

@codeslinger I just recently read an in-depth article about the traps of TRUE and FALSE. I learned/relearned how compiler's deal with non-false. Some compilers are more demanding than others, and most/many programmers/coders/copiers are woefully undereducated. My best advice is to use your compiler supplied TRUE and FALSE if provided BUT yes, do NOT test in the if, just do as @codeslinger says. Thanks for the reminder!

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
ron bentley
(@ronbentley1)
Member
Joined: 3 years ago
Posts: 385
 

@codeslinger 

I would usually declare my variable as bool(ean), so it would be 0 or 1, false or true, hence should not be an issue and if (x== trrue) , for eg, is a perfectly valid syntax and semantic.

 

 

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
ReplyQuote
Page 1 / 4