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 with adding to my 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 13 replies
  • Subscribers 392 subscribers
  • Views 999 views
  • Users 0 members are here
Related

Help with adding to my code

toxsickcity
toxsickcity over 9 years ago

Hi all,

 

 

I would like some help

Can someone guide me how to add functionality to my program to enable the arduino board to trigger an output when a set temperature is triggered.

 

I want to use my Sensors 1 and Sensor 2 to add 2 output triggers when a set temperature is reached with a minimum time to be triggered to stop relay hammer.

 

 

My Project is a WALL COMPUTER

 

I am building a special computer which has many thermal sensors and lighting effects for all components.

 

My plan is to have a cool lighting system and an automated fan speed system,

 

So when Video Card 1 / Video Card 2 / Power Supply / HDD / CPU / Water Temp, heat up

each device's lighting should glow red instead of blue, and want the relay to trigger.

 

I have brought a 2 channel Fully Isolated Photo Relay board and it has low or high level inputs to trigger the relays.

I have this part

 

http://www.ebay.com/itm/DC-5V-2-Channel-Double-Photo-Coupler-Isolated-Relay-H-L-Trigger-Layer-PCB-board-/201137319240

 

I have had much help making my arduino uno board control 2 LED strips cycle the Blue to RED  via thermal input.

It uses 6 PWM outputs and 2 Analog for the Sensors.

 

The program has been made from ideas of

http://www.jerome-bernard.com/blog/2013/01/12/rgb-led-strip-controlled-by-an-arduino/

 

I need to add code which I don't have yet. I don't know what code to use and don't know how to implement it to use the existing code I have

 

I am a noob and don't understand any of the code.

 

for example, I spent hours trying to remove green from the colour circle and failed.

I don't know what is going on with code..

 

I want to make the arduino board control both channels from my 2 thermos sensors.

 

at X temp. turn on

relay 1 must be controlled by sensor 1

relay 2 must be controlled by sensor 2

 

Any anyone can guide me I'd really appreciate this.

As this project is nearing completion and need my arduino working.

 

If you have any questions please don't hesitate to ask

 

Cheers

Shaun

Attachments:
code.txt.zip
  • Sign in to reply
  • Cancel

Top Replies

  • toxsickcity
    toxsickcity over 9 years ago in reply to mcb1 +1
    A Massive thank you for your help, I was too tired after work last night to fiddle but after today, its the weekend!! many thanks, I will report my findings tonight if I have enough energy!
  • mcb1
    mcb1 over 9 years ago

    toxsickcity

    Using someone else code is alaways tricky. You spend so much time figuring out what does waht.

     

    Firstly

    Do you have the 4k7 pullup resistor added between 5v and the data pin of the two sensors.?

    Without this you won't be able to talk with the sensor, and therefore no reading.

     

    If you open the Serial Terminal do you get either of the messages in Line 118 or 120?

     

     

    I haven't tried talking to two sensors but you don't need to discover them every time you want to read the temperature. Line 112, 158

     

    This is a sketch I did for controlling two relays using a single sensor (one was for heating the other cooling)

    /*
      Temp Controlled Relay
    
     DS18B20 Temperature chip on pin 2
     4k7 pullup between pin 2 and +5v.
    
     Pin assignments
     Pin 0  Rx
     Pin 1  Tx
     Pin 2  Temp sensor DS18B20
     Pin 7  Relay 1 output (Low to make it turn ON)
     Pin 8  Relay 2 output (Low to make it turn ON)
    
    
     Created 15 Nov 2012
     by Mark Beckett
    
        
     */
    
    // include the library code:
    
    #include <OneWire.h>
    
    // DS18B20 Temperature chip i/o
    OneWire ds(2);                     // TempSensor on pin 2
    byte i;
    byte present = 0;
    byte data[12];
    byte addr[8];
    word TReading;
    byte HighByte, LowByte;
    int SignBit, Tc_dec, Tc_100, Whole, Fract;
    
    //inputs
    const int TempSensor = 2;               // Tempsensor name
    
    //Outputs
    const int Relay1 = 7;               // Relay 1 Output
    const int Relay2 = 8;               // Relay 2 Output
    const int LedPin = 13;              // LED on pin 13
    
    //Settings
    int LowTempOn = 14;                   // Low Temperature On setting
    int LowTempOff = 24;                  // Low Temperature Off setting
    int HighTempOn = 29;                 // High Temperature On setting
    int HighTempOff = 25;                // High Temperature Off setting
    
    //-------------------------------------------------------------------------
    
    
    void setup() 
    {
       Serial.begin(57600);//Serial.begin(57600);
       
      //Define the pins
      pinMode(TempSensor, INPUT);
      pinMode(Relay1, OUTPUT);
      digitalWrite (Relay1, HIGH);    //Relay is OFF 
      pinMode(Relay2, OUTPUT);
      digitalWrite (Relay2, HIGH);    //Relay is OFF 
      
      !ds.search(addr);            //search for DS18B20 and identify its serial number
      Serial.println ("'The Shed' magazine Temperature Sensor example");
      Serial.println ("Refer to 2012 Dec/Jan issue for details");
    
    }
    
    void loop() 
    {
        Whole = 0;                //reset to a known value
        Fract = 0;                //reset to a known value
        Tc_dec =0;                //reset to a known value
        
        //reset and setup the temp sensor for communicating
            ds.reset();
            ds.select(addr);
            ds.write(0x44,1);        // start a temp reading
            
            delay(1000);             // wait 1 sec for data to be present
            
            present = ds.reset();
            ds.select(addr);    
            ds.write(0xBE);          // Read Scratchpad in the temp sensor
        
          for ( i = 0; i < 9; i++)   // we need 9 bytes
          {
            data[i] = ds.read();     //read the 9 bytes of data for a temperature      
          }
          
        Serial.print("Temp = ");
          
          LowByte = data[0];                       // the first byte is the lowest
          HighByte = data[1];                      // second byte is the highest
          TReading = (HighByte << 8) + LowByte;
          SignBit = TReading & 0x8000;             // test most sig bit
          if (SignBit)                             // If its negative
          {
             TReading = (TReading ^ 0xffff) + 1;   // 2's comp 
          }
            Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
    
            Whole = Tc_100 / 100;                  // separate off the whole portions
            Fract = Tc_100 % 100;                  // separate off the fraction portions
            
            if (SignBit)                           // If its negative
            {
             Serial.print("-");
            }
           Serial.print (Whole);
           //Serial.print(".");
           // if (Fract < 10)
           // {
           //  Serial.print("0");
           // }
           // Serial.print(Fract);
           Serial.print("\n");
          
          
           //Relays are ON when the ouput pin is LOW
           if (Whole <= LowTempOn)                              //reading is below or equal to the setting
           {
             digitalWrite(Relay1,LOW);
             digitalWrite(LedPin, HIGH);
           }
           if (Whole >= LowTempOff && !digitalRead(Relay1))     //reading is above or equal to the setting and Relay1 is ON
           {
             digitalWrite(Relay1,HIGH);
             digitalWrite(LedPin, LOW);
           }
           if (Whole >= HighTempOn)                              //reading is below or equal to the setting
           {
             digitalWrite(Relay2,LOW);
             digitalWrite(LedPin, HIGH);
           }
           if (Whole <= HighTempOff && !digitalRead(Relay2))    //reading is above or equal to the setting and Relay2 is ON
           {
             digitalWrite(Relay2,HIGH);
             digitalWrite(LedPin, LOW);
           } 
           if (digitalRead(Relay1) && digitalRead(Relay2))      // Flash the LED if either relay is High (Off).
           {
             digitalWrite(LedPin,!digitalRead(LedPin));
           }
    }

     

    You can see the temperature on the serial port

     

    Mark

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

    Hi, Thankyou for your reply.

     

    I do have a 4k7 on the sensors.

     

    Currently the arduino with this code does control 2 LED Strips with 2 sensors (each sensor control 1 LED Strip).

    I am very happy with that.

     

    I want to go with additional feature using the same 2 sensors to also control the that relay board.

     

    when I get home I will have a go at editing my code.

     

    Lastly, Can you explain to me how to view my arduino in a terminal window.

    It never works for me

    I read about it, and dont know why it displays nothing!

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

    Lastly, Can you explain to me how to view my arduino in a terminal window.

    As long as you have the Serial.begin(57600)

    You seem to have it twice Line 60 and 67

     

    and some Serial.print or Serial.println somewhere it should work.

    I'd add something like

         Serial.print("k :");

         Serial.println(((rgb[k] *bright[k])/256));

    to line 94 and 95

         Serial.print("k2 :");

         Serial.println(((rgb2[k] *bright[k])/256));

    to line 100 and 102 (or 104 and 106 as they'll become).

    which should show the values that are being sent to the rgb pins.

     

    I simply open the Serial Monitor

    image

    It will remember the last setting, so I tend to always use the same (9600) for my sketches as they are generally in debug mode and not timing critical.

     

     

    Try my sketch and you should get something.

     

    BTW Lines 59 and 66 are trying to read something on the Analogue pins ...not sure what but.

     

    Mark

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

    A Massive thank you for your help,

     

    I was too tired after work last night to fiddle but after today, its the weekend!! image

     

    many thanks,

    I will report my findings tonight if I have enough energy!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • toxsickcity
    toxsickcity over 9 years ago in reply to mcb1

    Hey Mark,

     

    I got the outputs working, it reads 5v on my selected pins when the Set thermal goes over, that's for 2 channels working independently.

     

    many thanks for your advise and your messages.

     

     

    I got another request, if you have time.

    I want to push my arduino board even harder and would save me a heap of space in my project if able to add another "Sensor/LED Strip/Relay Output".

     

    To be able to do this I would need to change my system from RGB To fade Blue to Red.

     

    This also means using only 2 not 3 PWM outputs for each LED Strip removing green which is exactly what I wanted from this project.

    I never intended to have green show up during the heat changes, imo for my project.

     

    Going from blue to green than to red looks a bit silly!!

     

    I could use some help only if you have time.

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

    I'll have a look, but first thought is you might be running out of PWM pins.

    Pins 3,5,6,9,10 and 11 are all you get.

     

    So in theory if you drop the green channel, you could have 3 x 2 channel Red/Blue.

    You could set the green component using resistors.

     

    As an alternative, have you considered using neoPixels (WS 2812) devices instead.

    One output pin can drive hundreds individually both colour and brightness.

    https://www.adafruit.com/?q=neopixel&

    These are 60 LED's per meter

    https://www.adafruit.com/products/1461

     

     

    Mark

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

    Hi Mark,

     

    Those LEDs are cool!

    I might have to buy some for future experiment.

     

    I would like to stay with my current design tho,

     

    Can you explain what you mean about the possible of keeping my green but using resistors?

     

    I don't want to keep green but am interested in hearing how you would go about keeping green when not using the pwm output?

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

    how you would go about keeping green when not using the pwm output

    The PWM alters how much voltage the LED receives. ie at 50% it is approximately half the current.

     

    You can use a digital Output to turn it ON or OFF and use two resistors to approximate the level you would have via a PWM output.

    The two resistors form a voltage divider so the 5v outout is dropped to a differen tvoltage depending on the ratio between the two.

    You need to be careful that the values aren't too high (or too low).

     

    Do a google search on Voltage divider and there will be online calculators ...

     

     

    Mark

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

    Oh I understand, but I would have required those digital out pins, and having 3 sets of Blue/Red would mean that I cannot. because the digital pins are being used to switch relays on and where the sensor connects.

     

    I will have the pins as such

    6x PWM = LED (Blue1/Red1, Blue2/Red2, Blue3/Red3)

    3x Sensor Inputs

    3x Relay Trigger outputs.

     

    so I think that is using all the digital pins on the Arduino Board.

     

    Playing with my code, I was able to change a line that said 0000FF00 to 00000000 and it removed green completely,

    but it didnt fade from blue to red nicely, it went off in between the 2 colours.

     

    I will leave it with you so see if you can come up with anything.

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

    Just a quick reply

    so I think that is using all the digital pins on the Arduino Board.

    The Analogue pins can also be used as Digital.

    A0 = D14

    A1 = D15

    A2 = D16

    A3 = D17

    A4 = D18

    A5 = D19

     

    Mark

    • 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