Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] IF statement doesn't do right
(@karol)
Eminent Member
Joined: 3 years ago
Posts: 12
Topic starter
March 23, 2021 7:02 AM
void CurrentSense(int Delay,int ReadDelay,int SensePin) {
Serial.print("Sensepin=");
Serial.print(SensePin);
I defined this function, which gets SensePin 1 or 0.
Although SensePin=0 or 1 is written, you will see by the output that only Solanoid is written all the time, regardless of SensePin:
Sensepin=0 Solanoid:
Reading=517 Ampere=1.53
Reading=464 Ampere=1.37
Reading=426 Ampere=1.26
Reading=396 Ampere=1.17
Reading=373 Ampere=1.10
Reading=355 Ampere=1.05
Sensepin=1 Solanoid:
Reading=323 Ampere=0.96
Reading=324 Ampere=0.96
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
March 23, 2021 7:59 AM
Hi Karol!
Could you post the code? Or that piece you're referring to?
(@karol)
Eminent Member
Joined: 3 years ago
Posts: 12
Topic starter
March 23, 2021 8:18 AM
// Channel A=Pump, Channel B=Solanoid
int PumpPWM=255; // Inhaling Power
int Delay_1=4200; // Inhale
int Delay_2=600; // Hold
int Delay_3=3500; // Exhale
int Delay_4=70; // Exhale-Spring Relief
int ReadDelay=500; // For the monitor, current sense
const int PumpPWM_Pin=3;
const int ExhaustPWM_Pin=11;
void setup() {
Serial.begin(9600);
pinMode(ExhaustPWM_Pin,OUTPUT);
pinMode(PumpPWM_Pin,OUTPUT);
pinMode(12,OUTPUT);
pinMode(0,INPUT);
pinMode(1,INPUT);
analogWrite(ExhaustPWM_Pin,255);
delay(Delay_3); // Emptying Bag, only at the beginning
analogWrite(ExhaustPWM_Pin,0);
}
void loop() {
analogWrite(PumpPWM_Pin,PumpPWM);
CurrentSense(Delay_1,ReadDelay,0);
delay(Delay_1); // Inhale
analogWrite(PumpPWM_Pin,0);
delay(Delay_2); // Hold
analogWrite(ExhaustPWM_Pin,255);
CurrentSense(Delay_3,ReadDelay,1);
delay(Delay_3); // Exhale
analogWrite(ExhaustPWM_Pin,0);
delay(Delay_4); // Exhale Spring Relief
}
void CurrentSense(int Delay,int ReadDelay,int SensePin) {
if(SensePin=0) { Serial.println("Pump:"); }
if(SensePin=1) { Serial.println("Solanoid:"); }
int Count=1;
int Elapsed=Count*ReadDelay;
while (Delay-Elapsed>0) {
int Reading=analogRead(SensePin);
Serial.print("Reading=");
Serial.print(Reading);
float Ampere=((5/3.3)*Reading)/511.5;
Serial.print("\tAmpere=");
Serial.println(Ampere);
delay(ReadDelay);
Count++;
Elapsed=Count*ReadDelay; }}
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
March 23, 2021 8:36 AM
I think this is where it goes wrong:
if(SensePin=0) { Serial.println("Pump:"); }
if(SensePin=1) { Serial.println("Solanoid:"); }
You're assigning the value 0 to SensePin, and you're not comparing. (see here for more info, scroll a little down wher you'll see the "Comparison Operators").
To compare we use "==" instead of "=" - common mistake 😉
Try this:
if(SensePin==0) { Serial.println("Pump:"); }
if(SensePin==1) { Serial.println("Solanoid:"); }
(@karol)
Eminent Member
Joined: 3 years ago
Posts: 12
Topic starter
March 23, 2021 9:06 AM
That's right, thanks, i found it in parallel with you.
The next question i am not sure of relates to AnalogRead.
I will post a new one because it's a different subject.
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
March 23, 2021 11:00 AM
Cool! Always satisfying when you find the answer yourself, isn't it? 😉