element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help adding some kind of heuristic to my relay portion of code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 9 replies
  • Subscribers 394 subscribers
  • Views 752 views
  • Users 0 members are here
  • arduino uno
Related

Help adding some kind of heuristic to my relay portion of code

toxsickcity
toxsickcity over 8 years ago

Hi all,

 

I have a small issue with my relays switching on and off every second or so, once it hits the temperature.

 

my code controls a arduino uno and using 3 onewire sensors

it controls 1 relay and Red/Blue LED lighting effect for each sensor

 

i have pulled code and added all kinds and to a experienced coder, you will laugh at it.. but it is working so i am happy...

 

 

i would essentially like to setup a somehow instead of having 1 temperature setting, have an upper and lower, instead of being only 30 degrees,

how can i have 30 on and 25 off

this will prevent relay hammer

 

here is the code i need help with...

if (temperature >30) {

digitalWrite(4,HIGH);

} else {

digitalWrite(4,LOW);

}

if (temperature2 >30) {

digitalWrite(8,HIGH);

} else {

digitalWrite(8,LOW);

}

if (temperature3 >30) {

digitalWrite(13,HIGH);

} else {

digitalWrite(13,LOW);

Attachments:
4722.code.txt.zip
  • Sign in to reply
  • Cancel
Parents
  • jw0752
    jw0752 over 8 years ago

    Hi Shaun,

    If you don't mind I would like to look at your actual circuit. Apart from the code there are things related to the electronics that can exacerbate this problem.

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • toxsickcity
    toxsickcity over 8 years ago in reply to jw0752

    Hi John, Mike and others..

     

    Ok so the circuit is for my MEGA BEAST COMPUTER. It's Finished and I have 2 USB leads out the back for updating code hahaha

     

    I wont able to open it but have an old photo that can explain it.

    I have referenced alot of my origonal design from the link below, I have since changed a fair bit to allow 1 channel of RGB LEDs and Sensor, to be RED/BLUE only,

    Then I changed the code to allow 3x Led Strips of RED/Blue, and changed to use 3 sensors, then changed to adding 3 relays

    Jerome Bernard: RGB Led Strip controlled by an Arduino

     

     

    So my photos show my design,

     

    http://imgur.com/a/QMyNK

     

    the picture of wiring showing

    1) The Arduinos

    2pcs of Arduinos, the 2 Boards are controlling 6x BLUE/RED Lighting, 6x Sensors, 6x Relays possible. tho I use only 4.

     

    2) The Mosfets

    1x Mosfet per LED Colour, 6 LED Strings x 2 Colours (RED and Blue) = 12x Mosfets total.

    6x Sensors also, please refer to the link above for wiring diagram

     

    3) The 2x DUAL OPTO Relay Boards

    These relay outputs are actually connected to Resistors! which connect to a DC-DC Converter 8A

    I have Desoldered the variable resistor and using the resistance from the array of resistors / relays to change FAN SPEED

     

    4) The 2x Power Supplies - DC-DC Converters 8A

    1st of them is powering Arduino and Relay Boards at 5v

    2nd is for the Fans!!!

     

     

    Up to date code,

    // HSV fade/bounce for Arduino - scruss.com - 2010/09/12
    // Note that there's some legacy code left in here which seems to do nothing
    // but should do no harm ...
    
    
    #include "OneWire.h"
    //#include "Streaming.h"
    
    
    const int DS18S20a_Pin = 2; //DS18S20 Signal pin on digital 2
    #define MIN_TEMP 0
    #define MAX_TEMP 50
    #define RELAY1  4 // Relay on digital pin 4
    OneWire ds(DS18S20a_Pin);
    
    
    const int DS18S20b_Pin = 7;
    #define MIN_TEMP2 0
    #define MAX_TEMP2 50
    //edit
    #define RELAY2  8
    OneWire dsb(DS18S20b_Pin);
    
    
    const int DS18S20c_Pin = 12;
    #define MIN_TEMP3 0
    #define MAX_TEMP3 50
    //edit
    #define RELAY3  13
    OneWire dsc(DS18S20c_Pin); 
    
    
    
    
    // don't futz with these, illicit sums later
    //#define RED       10// pin for red LED
    //#define RED2       3// pin for red LED
    
    
    #define SIZE    255
    #define SIZE2    255
    #define SIZE3   255
    
    
    #define DELAY    0
    #define DELAY2    0
    #define DELAY3    0
    
    
    #define HUE_MAX  6.0
    #define HUE2_MAX  6.0
    #define HUE3_MAX  6.0
    
    
    #define HUE_DELTA 0.01
    #define HUE2_DELTA 0.01
    #define HUE3_DELTA 0.01
    
    
    int firstPins [] = { 3,5};
    int secondPins [] = { 6,9};
    int thirdPins [] = { 10,11};
    
    
    //int secondPins [] = { 9,10};
    //long deltas[3] = { 5, 6, 7 };
    long rgb[2];
    long rgb2[3];
    long rgb3[3];
    long rgbval;
    long rgbval2;
    long rgbval3;
    // for reasons unknown, if value !=0, the LED doesn't light. Hmm ...
    // and saturation seems to be inverted
    float hue=0.0, saturation=1, value=1;
    float hue2=0.0, saturation2=1, value2=1;
    float hue3=0.0, saturation3=1, value3=1;
    
    
    /*
    chosen LED SparkFun sku: COM-09264
     has Max Luminosity (RGB): (2800, 6500, 1200)mcd
     so we normalize them all to 1200 mcd -
     R  250/600  =  107/256
     G  250/950  =   67/256
     B  250/250  =  256/256
     */
    long bright[2] = { 256, 256};
    long bright2[3] = { 256, 256};
    long bright3[3] = { 256, 256};
    //long bright[3] = { 256, 256, 256};
    
    
    long k, temp_value;
    
    
    void setup () {
      randomSeed(analogRead(4));
      pinMode(RELAY1, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(firstPins[k], OUTPUT);
        rgb[k]=0;
         analogWrite(firstPins[k], rgb[k] * bright[k]/256);
      }
      randomSeed(analogRead(4));
        pinMode(RELAY2, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(secondPins[k], OUTPUT);
        rgb2[k]=0;
        analogWrite(secondPins[k], rgb[k] * bright[k]/256);
      }
      randomSeed(analogRead(4));
        pinMode(RELAY3, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(thirdPins[k], OUTPUT);
        rgb3[k]=0;
        analogWrite(thirdPins[k], rgb[k] * bright[k]/256);
      }
    }
    //temperature1
    void loop() {
      pinMode(RELAY1, OUTPUT); //new
       pinMode(RELAY2, OUTPUT); //new
       pinMode(RELAY3, OUTPUT); //new
      float temperature = constrain(getTemp(), MIN_TEMP, MAX_TEMP);
      float temperature2 = constrain(getTemp2(), MIN_TEMP2, MAX_TEMP2);
      float temperature3 = constrain(getTemp3(), MIN_TEMP3, MAX_TEMP3);
      float deltaTemp = (MAX_TEMP - MIN_TEMP);
      float deltaTemp2 = (MAX_TEMP2 - MIN_TEMP2);
      float deltaTemp3 = (MAX_TEMP3 - MIN_TEMP3);
      float deltaHue = 4 - 0;
      float deltaHue2 = 4 - 0;
      float deltaHue3 = 4 - 0;
      hue = map((temperature - MIN_TEMP) * 100, 0, deltaTemp * 100, deltaHue * 100, 0) / 100.0;
      hue2 = map((temperature2 - MIN_TEMP2) * 100, 0, deltaTemp2 * 100, deltaHue2 * 100, 0) / 100.0;
      hue3 = map((temperature3 - MIN_TEMP3) * 100, 0, deltaTemp3 * 100, deltaHue3 * 100, 0) / 100.0;
      //Serial << "Temperature: " << temperature << endl;
      //Serial << "HUE: " << hue << endl;
      rgbval=HSV_to_RGB(hue, saturation, value);
      rgbval2=HSV_to_RGB2(hue2, saturation2, value2);
      rgbval3=HSV_to_RGB3(hue3, saturation3, value3);
      
      rgb[0] = (rgbval & 0x00FF0000) >> 16; // there must be better ways
      rgb[1] = (rgbval & 0x0000ff00) >> 8;
     // rgb[2] = rgbval & 0x000000FF; 
    for (k=0; k<2; k++) { // for all three colours
        analogWrite(firstPins[k], rgb[k] * bright[k]/256);}
    
    
      rgb2[0] = (rgbval2 & 0x00ff0000) >> 16; // there must be better ways
     rgb2[1] = (rgbval2 & 0x0000ff00) >> 8;
     // rgb2[1] = rgbval2 & 0x0000ff00;
       for (k=0; k<2; k++) { // for all three colours
            analogWrite(secondPins[k], rgb2[k] * bright[k]/256);}
    
      rgb3[0] = (rgbval3 & 0x00ff0000) >> 16; // there must be better ways
     rgb3[1] = (rgbval3 & 0x0000ff00) >> 8;
     // rgb2[1] = rgbval2 & 0x0000ff00;
       for (k=0; k<2; k++) { // for all three colours
            analogWrite(thirdPins[k], rgb3[k] * bright[k]/256);}
    
        //delay(DELAY);
    if (temperature >30) {
    digitalWrite(4,HIGH);
    } else {
    digitalWrite(4,LOW);
    }
    if (temperature2 >30) {
    digitalWrite(8,HIGH);
    } else {
    digitalWrite(8,LOW);
    }
    if (temperature3 >30) {
    digitalWrite(13,HIGH);
    } else {
    digitalWrite(13,LOW);
    }
    }
    
    
    // sensor1
    float getTemp(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !ds.search(addr)) {
       //no more sensors on chain, reset search
       ds.reset_search();
       return -10;
     }
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     ds.reset();
     ds.select(addr);
     ds.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = ds.reset();
     ds.select(addr);  
     ds.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = ds.read();
     }
    
     ds.reset_search();
    
     byte MSB = data[1];
     byte LSB = data[0];
    
    
     float tempRead = ((MSB << 8) | LSB); //using two's compliment
     float TemperatureSum = tempRead / 16;
    
     return TemperatureSum;
    }
    
    
    //2nd sensor
    
    
    float getTemp2(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !dsb.search(addr)) {
       //no more sensors on chain, reset search
       dsb.reset_search();
       return -10;
     }
    
    
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
    
    
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     dsb.reset();
     dsb.select(addr);
     dsb.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = dsb.reset();
     dsb.select(addr);  
     dsb.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = dsb.read();
     }
    
     dsb.reset_search();
    
     byte MSB2 = data[1];
     byte LSB2 = data[0];
    
    
     float temp2Read = ((MSB2 << 8) | LSB2); //using two's compliment
     float Temperature2Sum = temp2Read / 16;
    
     return Temperature2Sum;
    }
    
    
    //3nd sensor
    
    
    float getTemp3(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !dsc.search(addr)) {
       //no more sensors on chain, reset search
       dsc.reset_search();
       return -10;
     }
    
    
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
    
    
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     dsc.reset();
     dsc.select(addr);
     dsc.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = dsc.reset();
     dsc.select(addr);  
     dsc.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = dsc.read();
     }
    
     dsc.reset_search();
    
     byte MSB3 = data[1];
     byte LSB3 = data[0];
    
    
     float temp3Read = ((MSB3 << 8) | LSB3); //using two's compliment
     float Temperature3Sum = temp3Read / 16;
    
     return Temperature3Sum;
    }
    
    
    // End Sensors
    
    
    long HSV_to_RGB( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }
    //hsv 2
    long HSV_to_RGB2( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }
    //hsv 3
    long HSV_to_RGB3( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • toxsickcity
    toxsickcity over 8 years ago in reply to jw0752

    Hi John, Mike and others..

     

    Ok so the circuit is for my MEGA BEAST COMPUTER. It's Finished and I have 2 USB leads out the back for updating code hahaha

     

    I wont able to open it but have an old photo that can explain it.

    I have referenced alot of my origonal design from the link below, I have since changed a fair bit to allow 1 channel of RGB LEDs and Sensor, to be RED/BLUE only,

    Then I changed the code to allow 3x Led Strips of RED/Blue, and changed to use 3 sensors, then changed to adding 3 relays

    Jerome Bernard: RGB Led Strip controlled by an Arduino

     

     

    So my photos show my design,

     

    http://imgur.com/a/QMyNK

     

    the picture of wiring showing

    1) The Arduinos

    2pcs of Arduinos, the 2 Boards are controlling 6x BLUE/RED Lighting, 6x Sensors, 6x Relays possible. tho I use only 4.

     

    2) The Mosfets

    1x Mosfet per LED Colour, 6 LED Strings x 2 Colours (RED and Blue) = 12x Mosfets total.

    6x Sensors also, please refer to the link above for wiring diagram

     

    3) The 2x DUAL OPTO Relay Boards

    These relay outputs are actually connected to Resistors! which connect to a DC-DC Converter 8A

    I have Desoldered the variable resistor and using the resistance from the array of resistors / relays to change FAN SPEED

     

    4) The 2x Power Supplies - DC-DC Converters 8A

    1st of them is powering Arduino and Relay Boards at 5v

    2nd is for the Fans!!!

     

     

    Up to date code,

    // HSV fade/bounce for Arduino - scruss.com - 2010/09/12
    // Note that there's some legacy code left in here which seems to do nothing
    // but should do no harm ...
    
    
    #include "OneWire.h"
    //#include "Streaming.h"
    
    
    const int DS18S20a_Pin = 2; //DS18S20 Signal pin on digital 2
    #define MIN_TEMP 0
    #define MAX_TEMP 50
    #define RELAY1  4 // Relay on digital pin 4
    OneWire ds(DS18S20a_Pin);
    
    
    const int DS18S20b_Pin = 7;
    #define MIN_TEMP2 0
    #define MAX_TEMP2 50
    //edit
    #define RELAY2  8
    OneWire dsb(DS18S20b_Pin);
    
    
    const int DS18S20c_Pin = 12;
    #define MIN_TEMP3 0
    #define MAX_TEMP3 50
    //edit
    #define RELAY3  13
    OneWire dsc(DS18S20c_Pin); 
    
    
    
    
    // don't futz with these, illicit sums later
    //#define RED       10// pin for red LED
    //#define RED2       3// pin for red LED
    
    
    #define SIZE    255
    #define SIZE2    255
    #define SIZE3   255
    
    
    #define DELAY    0
    #define DELAY2    0
    #define DELAY3    0
    
    
    #define HUE_MAX  6.0
    #define HUE2_MAX  6.0
    #define HUE3_MAX  6.0
    
    
    #define HUE_DELTA 0.01
    #define HUE2_DELTA 0.01
    #define HUE3_DELTA 0.01
    
    
    int firstPins [] = { 3,5};
    int secondPins [] = { 6,9};
    int thirdPins [] = { 10,11};
    
    
    //int secondPins [] = { 9,10};
    //long deltas[3] = { 5, 6, 7 };
    long rgb[2];
    long rgb2[3];
    long rgb3[3];
    long rgbval;
    long rgbval2;
    long rgbval3;
    // for reasons unknown, if value !=0, the LED doesn't light. Hmm ...
    // and saturation seems to be inverted
    float hue=0.0, saturation=1, value=1;
    float hue2=0.0, saturation2=1, value2=1;
    float hue3=0.0, saturation3=1, value3=1;
    
    
    /*
    chosen LED SparkFun sku: COM-09264
     has Max Luminosity (RGB): (2800, 6500, 1200)mcd
     so we normalize them all to 1200 mcd -
     R  250/600  =  107/256
     G  250/950  =   67/256
     B  250/250  =  256/256
     */
    long bright[2] = { 256, 256};
    long bright2[3] = { 256, 256};
    long bright3[3] = { 256, 256};
    //long bright[3] = { 256, 256, 256};
    
    
    long k, temp_value;
    
    
    void setup () {
      randomSeed(analogRead(4));
      pinMode(RELAY1, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(firstPins[k], OUTPUT);
        rgb[k]=0;
         analogWrite(firstPins[k], rgb[k] * bright[k]/256);
      }
      randomSeed(analogRead(4));
        pinMode(RELAY2, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(secondPins[k], OUTPUT);
        rgb2[k]=0;
        analogWrite(secondPins[k], rgb[k] * bright[k]/256);
      }
      randomSeed(analogRead(4));
        pinMode(RELAY3, OUTPUT);
      Serial.begin(57600);
      for (k=0; k<2; k++) {
        pinMode(thirdPins[k], OUTPUT);
        rgb3[k]=0;
        analogWrite(thirdPins[k], rgb[k] * bright[k]/256);
      }
    }
    //temperature1
    void loop() {
      pinMode(RELAY1, OUTPUT); //new
       pinMode(RELAY2, OUTPUT); //new
       pinMode(RELAY3, OUTPUT); //new
      float temperature = constrain(getTemp(), MIN_TEMP, MAX_TEMP);
      float temperature2 = constrain(getTemp2(), MIN_TEMP2, MAX_TEMP2);
      float temperature3 = constrain(getTemp3(), MIN_TEMP3, MAX_TEMP3);
      float deltaTemp = (MAX_TEMP - MIN_TEMP);
      float deltaTemp2 = (MAX_TEMP2 - MIN_TEMP2);
      float deltaTemp3 = (MAX_TEMP3 - MIN_TEMP3);
      float deltaHue = 4 - 0;
      float deltaHue2 = 4 - 0;
      float deltaHue3 = 4 - 0;
      hue = map((temperature - MIN_TEMP) * 100, 0, deltaTemp * 100, deltaHue * 100, 0) / 100.0;
      hue2 = map((temperature2 - MIN_TEMP2) * 100, 0, deltaTemp2 * 100, deltaHue2 * 100, 0) / 100.0;
      hue3 = map((temperature3 - MIN_TEMP3) * 100, 0, deltaTemp3 * 100, deltaHue3 * 100, 0) / 100.0;
      //Serial << "Temperature: " << temperature << endl;
      //Serial << "HUE: " << hue << endl;
      rgbval=HSV_to_RGB(hue, saturation, value);
      rgbval2=HSV_to_RGB2(hue2, saturation2, value2);
      rgbval3=HSV_to_RGB3(hue3, saturation3, value3);
      
      rgb[0] = (rgbval & 0x00FF0000) >> 16; // there must be better ways
      rgb[1] = (rgbval & 0x0000ff00) >> 8;
     // rgb[2] = rgbval & 0x000000FF; 
    for (k=0; k<2; k++) { // for all three colours
        analogWrite(firstPins[k], rgb[k] * bright[k]/256);}
    
    
      rgb2[0] = (rgbval2 & 0x00ff0000) >> 16; // there must be better ways
     rgb2[1] = (rgbval2 & 0x0000ff00) >> 8;
     // rgb2[1] = rgbval2 & 0x0000ff00;
       for (k=0; k<2; k++) { // for all three colours
            analogWrite(secondPins[k], rgb2[k] * bright[k]/256);}
    
      rgb3[0] = (rgbval3 & 0x00ff0000) >> 16; // there must be better ways
     rgb3[1] = (rgbval3 & 0x0000ff00) >> 8;
     // rgb2[1] = rgbval2 & 0x0000ff00;
       for (k=0; k<2; k++) { // for all three colours
            analogWrite(thirdPins[k], rgb3[k] * bright[k]/256);}
    
        //delay(DELAY);
    if (temperature >30) {
    digitalWrite(4,HIGH);
    } else {
    digitalWrite(4,LOW);
    }
    if (temperature2 >30) {
    digitalWrite(8,HIGH);
    } else {
    digitalWrite(8,LOW);
    }
    if (temperature3 >30) {
    digitalWrite(13,HIGH);
    } else {
    digitalWrite(13,LOW);
    }
    }
    
    
    // sensor1
    float getTemp(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !ds.search(addr)) {
       //no more sensors on chain, reset search
       ds.reset_search();
       return -10;
     }
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     ds.reset();
     ds.select(addr);
     ds.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = ds.reset();
     ds.select(addr);  
     ds.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = ds.read();
     }
    
     ds.reset_search();
    
     byte MSB = data[1];
     byte LSB = data[0];
    
    
     float tempRead = ((MSB << 8) | LSB); //using two's compliment
     float TemperatureSum = tempRead / 16;
    
     return TemperatureSum;
    }
    
    
    //2nd sensor
    
    
    float getTemp2(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !dsb.search(addr)) {
       //no more sensors on chain, reset search
       dsb.reset_search();
       return -10;
     }
    
    
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
    
    
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     dsb.reset();
     dsb.select(addr);
     dsb.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = dsb.reset();
     dsb.select(addr);  
     dsb.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = dsb.read();
     }
    
     dsb.reset_search();
    
     byte MSB2 = data[1];
     byte LSB2 = data[0];
    
    
     float temp2Read = ((MSB2 << 8) | LSB2); //using two's compliment
     float Temperature2Sum = temp2Read / 16;
    
     return Temperature2Sum;
    }
    
    
    //3nd sensor
    
    
    float getTemp3(){
     //returns the temperature from one DS18S20 in DEG Celsius
    
    
     byte data[12];
     byte addr[8];
    
    
     if ( !dsc.search(addr)) {
       //no more sensors on chain, reset search
       dsc.reset_search();
       return -10;
     }
    
    
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
       return -10;
     }
    
    
     if ( addr[0] != 0x10 && addr[0] != 0x28) {
       Serial.print("Device is not recognized");
       return -10;
     }
    
    
     dsc.reset();
     dsc.select(addr);
     dsc.write(0x44,1); // start conversion, with parasite power on at the end
    
    
     byte present = dsc.reset();
     dsc.select(addr);  
     dsc.write(0xBE); // Read Scratchpad
    
    
    
     for (int i = 0; i < 9; i++) { // we need 9 bytes
      data[i] = dsc.read();
     }
    
     dsc.reset_search();
    
     byte MSB3 = data[1];
     byte LSB3 = data[0];
    
    
     float temp3Read = ((MSB3 << 8) | LSB3); //using two's compliment
     float Temperature3Sum = temp3Read / 16;
    
     return Temperature3Sum;
    }
    
    
    // End Sensors
    
    
    long HSV_to_RGB( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }
    //hsv 2
    long HSV_to_RGB2( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }
    //hsv 3
    long HSV_to_RGB3( float h, float s, float v ) {
      /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
      // H is given on [0, 6]. S and V are given on [0, 1].
      // RGB is returned as a 24-bit long #rrggbb
      int i;
      float m, n, f;
    
    
      // not very elegant way of dealing with out of range: return black
      if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
        return 0L;
      }
    
    
      if ((h < 0.0) || (h > 6.0)) {
        return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
      }
      i = floor(h);
      f = h - i;
      if ( !(i&1) ) {
        f = 1 - f; // if i is even
      }
      m = v * (1 - s);
      n = v * (1 - s * f);
      switch (i) {
      case 6:
      case 0: 
        return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
      case 1: 
        return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
      case 2: 
        return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
      case 3: 
        return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
      case 4: 
        return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
      case 5: 
        return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
      }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube