Hello All,
Please take a look at the code example. it is a modified sample from a Adafurit ina 260 breakout board.
The goal is a fast acting fuse operating through a 5V relay.
The ina 260 is working perfectly, it is reading on the serial monitor. I have tested it both with and without load and the ina 260 and the bench power supply agree.
What I am trying to do is read current for the "if" action later in the program and I would think that ina260.readCurrent would do it, but I have had no such luck. Today I have read and studied base, hexadecimal addresses ect and appears that my knowledge is still lacking, or to be more honest ,I have confused myself beyond belief, I just don't know how you guys keep it straight! Getting the basics in the discipline of "while" and "if" is like getting a drink from a fire hose.
In my research I noticed that the Drone Bot Workshop did a piece on the ina 219, so I took a look at the code and there is a lot of "float which make sense to me because the current is a moving target. Why not the ina 260?
The "if" statement that caused a interesting discussion of true and false needs to be compared to the current flow, but I will be *%$#! if I can figure out how to get it there. In analog, in one pin, out the other. The base question is, how do I get the information from my ina 260 with I2C connection ( A 4,5) to my output D9 and what needs to be included in the code?
#include <Adafruit_Sensor.h> #include <Wire.h> #include <Adafruit_INA260.h> int mypinout = (9); Adafruit_INA260 ina260 = Adafruit_INA260(); void setup() { Serial.begin(115200); // Wait until serial port is opened while (!Serial) { delay(10); pinMode(mypinout, OUTPUT); digitalWrite(mypinout, LOW); } Serial.println("Adafruit INA260 Test"); if (!ina260.begin()) { Serial.println("Couldn't find INA260 chip"); while (1); } Serial.println("Found INA260 chip"); } void loop() { 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 ( <, 2.500) { digitalWrite(mypinout, HIGH); } delay(100); if ( ,>, 2.500) { digitalWrite(mypinout, LOW); } Serial.println(); delay(500 ); }
As always, any help is appreciated,any knowledge treasured. Thanks, Quince
An if statement looks like the following, yours are different
if (A > B) {
do something
} else {
do something else even if nothing
}
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.
BTW, the compare operator can be other things like less, equal, not equal etc. Check the doc'n for the complete answer.
Manual is at https://www.arduino.cc/reference/en/
Here is one thing you will see
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.
Just a quick look at the code and I think you should be doing something like
current = float;
current = ina260.readCurrent());
Serial.print(current);
and then later
if ( current < 2.500){
whatever you want to do.
}
@byron Maybe I missed something, but is 'current = float;' allowed? Did you mean 'current float;'
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.
(@quince) Your "if" statement appears to be missing the value parameter you wish to compare to the 2.500
Try something like (with XXXX being the parameter you want to compare to the 2.5). In your case I think it is the current value from the INA260 module. You will also have to declare that value parameter before you can use it.
if (xxxx =< 2.500) { // making sure current is 2.500 or lower
digitalWrite (mypinout, HIGH);
}
else if (XXXX > 2.500) { // when current is above 2.5 turn off output
digitalWrite (mypinout, LOW);
}
See the Arduino website examples: else - Arduino Reference
RCC1
@byron Maybe I missed something, but is 'current = float;' allowed? Did you mean 'current float;'
Other way round?
float current;
@yurkshirelad Well I got the no = part right, part marks?
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.
Maybe I missed something, but is 'current = float;' allowed? Did you mean 'current float;'
Woops, just come from a little python coding and a fuddled brain.
here is a better snippet
void setup() {
Serial.begin(9600);
}
void loop()
{
float current;
current = 3.5;
Serial.print(current);
if (current < 2.500)
{
Serial.println("its less");
}
else
{
Serial.println("its more");
}
}
@yurkshirelad Well I got the no = part right, part marks?
Yep! 👍 It could be combined:
float current = ina260.readCurrent());
Fuddled brain? Been there, done that over the recent years when coding in Java, Python and Javascript on the same project. 😱
Fuddled brain? Been there, done that
😋 . I like to keep my hand in with c++ coding, but these days I mostly stick with python or micropython. Swapping to C++ from python it takes a hour or two to remember to put in those pesky ; Swapping the other way and I forget the : after the if and def and the like. It soon settles down though. I don't think I could cope with 3 different languages on the trot.
Thank you all for the input. I will try the solution given. It makes sense to me. I do not know, is it possible to hit a moving target like fluctuating current in C++ with anything but a "float"? I think my problem is I did not know the command/structure to allow the information to be received by the program from the I2C and acted on later in the "if" statement. I had tried several things before asking for guidance, next time I will avoid the confusion. Thanks again, Quince
float current = ina260.readCurrent());
it must have been a fuddled evening for us, I expect its obvious but theres a ) too many in the bit of code you put in your post as copied from our posts. 😮
Please do not think anything is obvious, I have been working with code for 90 days. If you see something,please feel free to point it out. I am not kidding when I say the ability to ask a question of someone who knows is priceless.
Thanks Quince