Page 1 of 1
Forum

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 most likely no translated for the post!




IF statement doesn'...
 
Share:
Notifications
Clear all

IF statement doesn't do right


(@karol)
Active Member
Joined: 1 month ago
Posts: 8
Topic starter  
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


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
 

Hi Karol!

Could you post the code? Or that piece you're referring to?


ReplyQuote
(@karol)
Active Member
Joined: 1 month ago
Posts: 8
Topic starter  
// 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; }}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
 

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:"); }

ReplyQuote
(@karol)
Active Member
Joined: 1 month ago
Posts: 8
Topic starter  

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.


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
 

Cool! Always satisfying when you find the answer yourself, isn't it? 😉 


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: