Page 1 of 1

Invert parabola

Posted: Thu Feb 28, 2013 9:05 am
by DrewPalmer04
New driver is 0v=100%

How can I invert the PWMParabola in arduino?

Re: Invert parabola

Posted: Thu Feb 28, 2013 9:13 am
by rimai
Is that steve's?
Inverted?
Buckpucks are inverted too :(

Re: Invert parabola

Posted: Thu Feb 28, 2013 9:48 am
by DrewPalmer04
Testing buckpack. Anyway to invert PWMParabola? Start at 255 instead of 0. Basically flop it.

Re: Invert parabola

Posted: Thu Feb 28, 2013 9:51 am
by rimai
I thought you were going with Steve's?? Changed your mind?

Re: Invert parabola

Posted: Thu Feb 28, 2013 10:40 am
by DrewPalmer04
I'm just trying options. I still have the meanwells too and working on the op amp for them. But I like the idea of 0v=100% just if/when there is a code panic or failure. My lights will at least be on and not off. Ya know?

Re: Invert parabola

Posted: Thu Feb 28, 2013 10:43 am
by rimai
Which buckpuck did you get?
For the inverted number, how do invert a number from 100-0 instead of 0-100?? Think about it and let's see if you can give me this answer :)

Re: Invert parabola

Posted: Thu Feb 28, 2013 11:57 am
by DrewPalmer04
I'll have to look at the model..a friend of mine gave it to me for testing IF I like it or not.

I'm guessing the code would go a little something like this:

Code: Select all

#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

int blue1 = 9;             // blue LEDs connected to digital pin 9 (pwm)
int white1 = 6;            // white LEDs connected to digital pin 6 (pwm)
byte InvertActinic=0;
byte InvertDaylight=0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(192,168,1, XXX);


EthernetServer server(84);
/* ******** NTP Server Settings NTP BEGIN ******** */

IPAddress timeServer(50, 77, 217, 185); 
IPAddress backupNTP(64, 113, 32, 5); // back up NTP sever

/* Set this to the offset (in seconds) to your local time
 This example is GMT - 6 */
const long timeZoneOffset = -21600L;  

/* Syncs to NTP server every 15 seconds for testing,
 set to 1 hour or more to be reasonable */
unsigned int ntpSyncTime = 3600; //3600 one hour     


/* ALTER THESE VARIABLES AT YOUR OWN RISK */
// local port to listen for UDP packets
unsigned int localPort = 8888;
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE= 48;      
// Buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];  
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;                    
// Keeps track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;    
// Check last time clock displayed (Not in Production)

// Do not alter this function, it is used by the system
int getTimeAndDate() {
  int flag=0;
  Udp.begin(localPort);
  sendNTPpacket(timeServer);
  delay(1000);
  if (Udp.parsePacket()){
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    unsigned long highWord, lowWord, epoch;
    highWord = word(packetBuffer[40], packetBuffer[41]);
    lowWord = word(packetBuffer[42], packetBuffer[43]);  
    epoch = highWord << 16 | lowWord;
    epoch = epoch - 2208988800 + timeZoneOffset;
    flag=1;
    setTime(epoch);
    ntpLastUpdate = now();
  }
  return flag;
}

// Do not alter this function, it is used by the system
unsigned long sendNTPpacket(IPAddress& address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  packetBuffer[0] = 0b11100011;
  packetBuffer[1] = 0;
  packetBuffer[2] = 6;
  packetBuffer[3] = 0xEC;
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;                  
  Udp.beginPacket(address, 123);
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

//NTP END

/*PWM START*/
byte intlength(int intin)

{

  if (intin>9999) return 5;

  if (intin>999) return 4;

  if (intin>99) return 3;

  if (intin>9) return 2;

  if (intin>=0 && intin<=9) return 1;

  if (intin<0) return 2;

}



int NumMins(uint8_t ScheduleHour, uint8_t ScheduleMinute)

{

  return (ScheduleHour*60) + ScheduleMinute;

}



bool IsLeapYear(int year)

{

  if (year % 4 != 0)

  {

    return false;

  }

  else if (year % 400 == 0)

  {

    return true;

  }

  else if (year % 100 == 0)

  {

    return false;

  }

  return true;

}



byte PWMSlope(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)

{

  int Now = NumMins(hour(), minute());

  int Start = NumMins(startHour, startMinute);

  int StartD = Start + Duration;

  int End = NumMins(endHour, endMinute);

  int StopD = End - Duration;



  if ( Now >= Start && Now <= StartD )

    return constrain(map(Now, Start, StartD, startPWM, endPWM),startPWM, endPWM);

  else if ( Now >= StopD && Now <= End )

  {

    byte v = constrain(map(Now, StopD, End, startPWM, endPWM),startPWM, endPWM);

    return endPWM-v+startPWM;

  }

  else if ( Now > StartD && Now < StopD )

    return endPWM;



  // lastly return the existing value

  return oldValue;

}



byte PWMParabola(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte oldValue)

{

  int Now = NumMins(hour(), minute());

  int Start = NumMins(startHour, startMinute);

  int End = NumMins(endHour, endMinute);

  byte PWMDelta = endPWM-startPWM;

  byte ParabolaPhase=constrain(map(Now,Start,End,0,180),0,180);



  if ( Now <= Start || Now >= End)

    return oldValue;

  else

  {

    return startPWM+(PWMDelta*sin(radians(ParabolaPhase)));

  }



}

/*PWM END*/

void NTP()//loop NTP with one hour refresh
{
  if(now()-ntpLastUpdate > ntpSyncTime) {
    int trys=0;
    while(!getTimeAndDate() && trys<10){
      trys++;
    }
    if(trys<10)
    {
      // Serial.println("ntp server update success");
    }
    else if (trys>=10 && trys<20)
    {
      sendNTPpacket(backupNTP);
      // Serial.println("backup success");
    }
    else
    {
      // Serial.println("ntp server update failed");
    }
  }


}

void setup()
{

  Ethernet.begin(mac, ip);
  server.begin();

  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  int trys=0;
  while(!getTimeAndDate() && trys<10) {
    trys++;
  }

}

void loop()
{
  NTP();
  InvertActinic=(2.55*(PWMParabola(12,1,14,10,22,100,InvertActinic)));
  InvertDaylight=(2.55*(PWMParabola(6,1,21,10,22,100,InvertDaylight)));
  analogWrite(blue1, 255-(InvertActinic));
  analogWrite(white1, 255-(InvertDaylight));

}


YES, NO, MAYBE SO? :)

Re: Invert parabola

Posted: Thu Feb 28, 2013 12:22 pm
by rimai
Yeap :)
You got it

Re: Invert parabola

Posted: Thu Feb 28, 2013 12:38 pm
by lnevo
Can you sum that up in one or two lines of code?

Re: Invert parabola

Posted: Thu Feb 28, 2013 12:41 pm
by rimai
Inverting a number from 0-100 to 100-0 is simple as:
x=100-x;
:)

Re: Invert parabola

Posted: Thu Feb 28, 2013 12:49 pm
by lnevo
I mean having the parabola go from 100 to 0... but I'll look through his code :)

Re: Invert parabola

Posted: Thu Feb 28, 2013 12:51 pm
by DrewPalmer04
lnevo wrote:Can you sum that up in one or two lines of code?

Sorry. I have to add that NTP junk to get the time before I can test anything. (I need to buy an RTC) :(

Mines a little different because the PWMSlope & PWMParabola output a percentage...so I have to 2.55*(blah)

So instead of x=100-x; I have to do x=255-x;
I'm using Arduino not the RA..so it has to be 0-255 "steps."
You can see I have to analogWrite(pin, value)...for RA it's different.


So, to sum it up....

Code: Select all

int blue1 = 9;             // pin 9 (pwm)
int white1 = 6;           // pin 6 (pwm)
byte InvertActinic=0; //stores variable for InvertActinic
byte InvertDaylight=0; //stores variable for InvertDaylight

void setup(){

}


void loop() {
  InvertActinic=(2.55*(PWMParabola(12,1,14,10,22,100,InvertActinic))); //Tell what InvertActinic is 
  InvertDaylight=(2.55*(PWMParabola(6,1,21,10,22,100,InvertDaylight))); //Tell what InvertDaylight is
  analogWrite(blue1, 255-(InvertActinic)); //What to do with InvertActinic value
  analogWrite(white1, 255-(InvertDaylight)); //What to do with InvertDaylight value

}


Re: Invert parabola

Posted: Thu Feb 28, 2013 12:56 pm
by lnevo
Got it.. thanks :)

Re: Invert parabola

Posted: Thu Feb 28, 2013 1:18 pm
by DrewPalmer04
Cool beans. I think Roberto is on to me. He makes me "earn it" lol!!! :o

Re: Invert parabola

Posted: Thu Feb 28, 2013 1:21 pm
by rimai
:) lol