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 no translation for that post!



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

[Solved] IF statement doesn't do right

6 Posts
2 Users
0 Likes
825 Views
(@karol)
Eminent Member
Joined: 3 years ago
Posts: 12
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: 11 years ago
Posts: 1065
 

Hi Karol!

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


   
ReplyQuote
(@karol)
Eminent Member
Joined: 3 years ago
Posts: 12
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: 11 years ago
Posts: 1065
 

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)
Eminent Member
Joined: 3 years ago
Posts: 12
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: 11 years ago
Posts: 1065
 

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


   
ReplyQuote
Share: