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
  • michaelkellett
    michaelkellett over 8 years ago

    if (temperature >30) {

    digitalWrite(4,HIGH);

    } else if (temperature < 25) {

    digitalWrite(4,LOW);

    }

     

    I don't code for or use Arduino (so this may not be valid code for an else if)  but the principle is universal.

    The other thing you can try is to run the test at longer intervals.

     

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • beacon_dave
    beacon_dave over 8 years ago in reply to michaelkellett

    Yes, it is valid for an Arduino

    https://www.arduino.cc/en/Reference/Else

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

    thanks guys,

     

    I will give it a go

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

    What MK wrote will work.  If you want to clean it up in CS terms, have a coupla variables, maybe Cross_Point and Hysteresis, use a formalism.  Then when you want to change these params you won't have to go digging through the code

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 8 years ago in reply to D_Hersey

    The original code the OP wrote should also work.

     

    Any chance you can include all the code as I suspect something else is causing the drop.

     

     

    Mark

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

    Hi mark,

    I did give the else if a go, but seems it didnt help much.. it still hammered.. like on off on off for upto a minute changing state every 1-2 seconds

     

    I would like to simply use original code and use a Minimum time the relay stays on, which will prevent hammer...

     

    I was told to look at this and am confused how to implement this into my code.

     

    /* Blink without Delay
    
     Turns on and off a light emitting diode (LED) connected to a digital
     pin, without using the delay() function.  This means that other code
     can run at the same time without being interrupted by the LED code.
    
     The circuit:
     * LED attached from pin 13 to ground.
     * Note: on most Arduinos, there is already an LED on the board
     that's attached to pin 13, so no hardware is needed for this example.
    
     created 2005
     by David A. Mellis
     modified 8 Feb 2010
     by Paul Stoffregen
     modified 11 Nov 2013
     by Scott Fitzgerald
    
    
     This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
     */
    
    // constants won't change. Used here to set a pin number :
    const int ledPin =  13;      // the number of the LED pin
    
    // Variables will change :
    int ledState = LOW;             // ledState used to set the LED
    
    // Generally, you should use "unsigned long" for variables that hold time
    // The value will quickly become too large for an int to store
    unsigned long previousMillis = 0;        // will store last time LED was updated
    
    // constants won't change :
    const long interval = 1000;           // interval at which to blink (milliseconds)
    
    void setup() {
      // set the digital pin as output:
      pinMode(ledPin, OUTPUT);
    }
    
    void loop() {
      // here is where you'd put code that needs to be running all the time.
    
      // check to see if it's time to blink the LED; that is, if the
      // difference between the current time and last time you blinked
      // the LED is bigger than the interval at which you want to
      // blink the LED.
      unsigned long currentMillis = millis();
    
      if (currentMillis - previousMillis >= interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
    
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
    
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
      }
    }

     

     

    Here is MY CODE image

     

    // 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 10
    #define MAX_TEMP 45
    #define RELAY1  4 // Relay on digital pin 4
    OneWire ds(DS18S20a_Pin);
    
    
    const int DS18S20b_Pin = 7;
    #define MIN_TEMP2 10
    #define MAX_TEMP2 45
    //edit
    #define RELAY2  8
    OneWire dsb(DS18S20b_Pin);
    
    
    const int DS18S20c_Pin = 12;
    #define MIN_TEMP3 10
    #define MAX_TEMP3 45
    //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 < 40) {
    digitalWrite(4,HIGH);
    } else {
    digitalWrite(4,LOW);
    }
    
    
    if (temperature2 > 40) {
    digitalWrite(8,HIGH);
    } else {
    digitalWrite(8,LOW);
    }
    if (temperature3 > 40) {
    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);
      }
    }

     

    Cheers

     

    I noticed my code here is older, I have few issues such as the > and < in the relay part, the first is different to temperature2 and temperature3

     

    I will Dump a new code when I get home, sorry for this confusing anyone haha

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

    At first glance there are a few things that are not right.

     

    Line 99 you define the relay outputs  pinMode(RELAY1, OUTPUT);

    However you haven't declared what pin Relay1 is attached to.

     

    same again for Line 107, and 115.

     

    I don't think declaring the outputs in the loop section is a good idea.

    While you can change between an input and output the state of the pin at the time may change.

     

    It has the comment /new after it so I presume it was an attempt to overcome why it was cycling.

     

    At line 167 you are changing the state of Pins 4, 8 and 13, which presumably have the relays attached.

    But these pins are not declared as Outputs, and will not be getting driven with the full current.

     

    Your declaration (void setup() ) also has some extras.

    Lines 98, 106, 114 are the same and it only needs 1.

    Lines 100, 108, 116 also are the same, again only 1 reqd.

     

    I think there is some work also necessary on the getTemp routines.

    You seem to do a search and reset at each time you call the routine.

    In practice you really need to do the search, and then make the call to read the temperature.

    For some readings it can take upto 750mS, so I always set it up to do the call and then check if 1 sec has gone, and keep coming back.

     

    I suspect that this may be why the relays are dropping out.

    basically the call to read temp is returning a vause that is not within the range, and therefore it is dropping out (as it should).

     

     

    You could prove that by adding these lines at line 165

    Serial.print("temperature = ");

    Serial.println(temperature);

    Serial.print("temperature2 = ");

    Serial.println(temperature2);

    Serial.print("temperature3 = ");

    Serial.println(temperature3);

     

    When you run it select Serial Monitor (set speed to 57600 to match line 100)

    It will display the readings.

     

    Cheers

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • 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
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