If Port On Then

Do you have a question on how to do something.
Ask in here.
Post Reply
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

If Port On Then

Post by clw143 »

It seems I am not using this correctly.

Code: Select all

  // Dimmable royal blues 100% if actinics (relay port 2) are on, else moonlight
  if bitRead(ReefAngel.Relay.RelayMaskOn, Port2Bit) {
    ReefAngel.PWM.SetActinic( 100 );
  }
  else {
    ReefAngel.PWM.SetActinic( MoonPhase() );
  }
I get 100% all the time, what is the proper usage?
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

Should it be Port1Bit for port 2?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

Its should be 1 actually.

You are looking to read bit 0-7 to translate 1-8. I kept making that mistake a lot to use the Port2Bit
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

If you are on latest library there should be a function you can use...

ReefAngel.Relay.IsMaskOn(Port);

And also an IsMaskoff(Port)

So you dont have to worry about bits
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

Thanks, It was a little confusing seeing this:

Code: Select all

  // Ports toggled in Water Change Mode
  ReefAngel.WaterChangePorts = Port3Bit | Port4Bit | Port5Bit | Port6Bit | Port7Bit | Port8Bit;
The wizard puts Port#Bit = port number, but in using the same Port#Bit in a bit read is offset by one.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

No, the portxbit does not equal port number. It represents the bit set in binary...so port3bit = 00000100 which is equal to 4 i believe.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

Right, I follow, BUT then why does the wizard make a Port8Bit? It should not exist. For my water change mode above, ports 3-8 are off and Port3Bit - Port8Bit are specified.

I have version 1.0.9

It has no idea what IsMaskOn is.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

It might still be in the dev right now, I'll check tomorrow.

The port bit is used to set things like feeding mode ports and the values are logical ORed together..

FeedingModePorts = Port1 | Port2;
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

If you look at what port3bit is lets look at the binary number again..

00000100

Now if we want to check if that bit is set we want to use bitRead to check we would read the 3rd bit counting from the right but starting at 0 as if it were an array so the first 0 on the right would be bitread(Port3Bit, 0);

The next would be bit 1 the next (Port3Bit) sould be bit 2.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: If Port On Then

Post by rimai »

I think you are getting confused with the number and definition of the PortXBit....
This is from Globals.h:

Code: Select all

// Outlets on Relay box
#define Port8   8
#define Port7   7
#define Port6   6
#define Port5   5
#define Port4   4
#define Port3   3
#define Port2   2
#define Port1   1

// Port bits
#define Port8Bit   1<<7
#define Port7Bit   1<<6
#define Port6Bit   1<<5
#define Port5Bit   1<<4
#define Port4Bit   1<<3
#define Port3Bit   1<<2
#define Port2Bit   1<<1
#define Port1Bit   1<<0
Which define easier to use numbers, so we don't need to do the bit shifting or bit manipulation manually.
These numbers are meant for use on our functions.
So, Port5Bit is the same as binary B00010000, which is the same as hex 0x10, which is the same as decimal 16.
If you use a function like this:

Code: Select all

ReefAngel.FeedingModePorts = Port4Bit;
It will be the same as this:

Code: Select all

ReefAngel.FeedingModePorts = 16;
Now, if you look at the bitRead function here: http://arduino.cc/en/Reference/bitRead
You will see that the function calls for a bit position and not the decimal value of where the bit is located in a byte variable.
Bit shifting and bit math can be a little confusing at first.
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

clw143 wrote:Right, I follow, BUT then why does the wizard make a Port8Bit? It should not exist. For my water change mode above, ports 3-8 are off and Port3Bit - Port8Bit are specified.

I have version 1.0.9

It has no idea what IsMaskOn is.
I'm 99.9% sure its in there. Let me verify the syntax. I just saw someone post code that was using it at 1.0.9
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

It is in the stock 1.0.9 I just confirmed.

Take a look at the class definition for Relay.

http://www.easte.net/RA/html/class_relay_class.html#

The syntax should be:

Code: Select all

ReefAngel.Relay.IsMaskOn(PortX);
or

Code: Select all

ReefAngel.Relay.IsMaskOff(PortX);
Make sure to change X :)
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

I was having some weird results and changed the code to this

Code: Select all

  // Dimmable royal blues 100% if actinics (relay port 2) are on, else moonlight
  if (bitRead(ReefAngel.Relay.RelayMaskOn, Port1Bit)) ReefAngel.PWM.SetActinic( 100 );
  else ReefAngel.PWM.SetActinic( MoonPhase()/2 );
Moonlights should be 100% if port 2 is on, and that is quite literally how it is working, if it is in "auto" or "off" it is in moonlight mode, if "on" it is 100%

How do I get them to work right in auto?
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: If Port On Then

Post by rimai »

Use RelayData instead of RelayMaskOn
Roberto.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

lnevo wrote:It is in the stock 1.0.9 I just confirmed.

Take a look at the class definition for Relay.

http://www.easte.net/RA/html/class_relay_class.html#

The syntax should be:

Code: Select all

ReefAngel.Relay.IsMaskOn(PortX);
or

Code: Select all

ReefAngel.Relay.IsMaskOff(PortX);
Make sure to change X :)
I try this:

Code: Select all

  // Dimmable royal blues 100% if actinics (relay port 2) are on, else moonlight
  if (ReefAngel.Relay.IsMaskOn(Port2);) ReefAngel.PWM.SetActinic( 100 );
  else ReefAngel.PWM.SetActinic( MoonPhase()/2 );
Error says: 'class RelayClass' has no member named 'IsMaskOn'
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: If Port On Then

Post by rimai »

Humm..
That may have been my fault.
I think I forgot to add the entries in the features.txt file.
Hey Lee, can you create an issue and include the entries that need to added?
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: If Port On Then

Post by lnevo »

Its a lowercase is...sorry

ReefAngel.Relay.isMaskOn(Port)

My bad, no feature.txt required.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: If Port On Then

Post by clw143 »

This works
rimai wrote:Use RelayData instead of RelayMaskOn

This is still off for auto when auto is on and off for off, and on for on. I need on when auto on. (if that's not a confusing statement)

Code: Select all

  // Dimmable royal blues 100% if actinics (relay port 2) are on, else moonlight
  if (ReefAngel.Relay.isMaskOn(Port2)) ReefAngel.PWM.SetActinic( 100 );
  else ReefAngel.PWM.SetActinic( MoonPhase()/5 );
Post Reply