Arduino UNO pool control code problem

Discussion in 'Programmer's Corner' started by aouate3, Apr 17, 2011.

  1. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    Hello all, First off, i am a noob when it comes to the arduino platform, i ordered a Arduino UNO and i will be recieving it in the next few weeks, I am working on setting up a control system for my pool, and also our outdoor lighting, there are 4 relays i have wired, one for main pump, one for booster pump, one for spa blower, and one for the outdoor lighting, the relays are wired to the diode and transistor so they can be controlled by the arduino, all the wiring is setup and should be fine, i have even put a schematic on here if it helps at all, I am using the arduino 0022 software to write the code, i have copied the code from http://www.atari-source.org/node/65 and modifed it for my own uses, but i am unable to compile it be cause it errors out, same with the code when i copy it directly from the website in its full, unaltered state...

    also in the schematic, there are two temperature sensors, which i havent coded yet, because i simply, do not know how to do it...


    Here is my code so far

    #include
    int state_FilterPump=0, state_BoosterPump=0, state_SpaBlower=0, state_Lighting=0;
    bool have_control=false; // Temp variable
    int control_relay=0; // Temp variable

    void setup()
    {
    pinMode(2, OUTPUT); // sets the digital pin as output
    pinMode(4, OUTPUT); // sets the digital pin as output
    pinMode(7, OUTPUT); // sets the digital pin as output
    pinMode(8, OUTPUT); // sets the digital pin as ouput

    // Prepare the serial ports
    Serial.begin(9600);
    Serial.flush();

    }
    void loop()
    {
    int serial_byte=0;

    if (Serial.available()>0)
    {
    /* Here we wait for the first byte of a two byte control sequence. Send A, B or C to control filterpump,boosterpump, light, or blower.
    */

    serial_byte=Serial.read();
    if (!have_control)
    {
    have_control=true;
    if (serial_byte=='A')
    control_relay=2
    else if (serial_byte=='B')
    control_relay=4
    else if (serial_byte=='C')
    control_relay=7;
    else if (serial_byte=='D')
    control_relay=8;

    // We have the control byte, now lets look for 1 or 0
    if (serial_byte=='1')
    {
    // Turn specified relay on
    if (control_relay==2
    state_FilterPump=HIGH;
    if (control_relay==4
    state_BoosterPump=High;
    if (control_relay==7
    state_SpaBlower=HIGH;
    if (control_relay==8
    state_Lighting=HIGH;
    }
    else if (serial_byte=='0')
    {
    // Turn specified relay off
    if (control_relay==2)
    state_FilterPump=LOW;
    if (control_relay==4)
    state_BoosterPump=LOW;
    if (control_relay==7)
    state_SpaBlower=LOW;
    If (control_relay==8)
    state_Lighting=LOW;
    }






    What am i doing wrong, why cant i compile it, and is there any way to make it simpler? because of the fact i dont have the arduino yet, i can only assume i would turn on the relays by issueing a A1, B1, C1, or D1 command, and turn off the relays by issueing a A0, B0, C0, or D0 command
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Here is my code so far
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include
    4. int state_FilterPump=0, state_BoosterPump=0, state_SpaBlower=0, state_Lighting=0;
    5. bool have_control=false; // Temp variable
    6. int control_relay=0; // Temp variable
    7.  
    8. void setup()
    9. {
    10. pinMode(2, OUTPUT); // sets the digital pin as output
    11. pinMode(4, OUTPUT); // sets the digital pin as output
    12. pinMode(7, OUTPUT); // sets the digital pin as output
    13. pinMode(8, OUTPUT); // sets the digital pin as ouput
    14.  
    15. // Prepare the serial ports
    16. Serial.begin(9600);
    17. Serial.flush();
    18.  
    19. }
    20. void loop()
    21. {
    22. int serial_byte=0;
    23.  
    24. if (Serial.available()>0)
    25. {
    26. /* Here we wait for the first byte of a two byte control sequence. Send A, B or C to control filterpump,boosterpump, light, or blower.
    27. */
    28.  
    29. serial_byte=Serial.read();
    30. if (!have_control)
    31. {
    32. have_control=true;
    33. if (serial_byte=='A')
    34. control_relay=2[COLOR=red][B];[/B][/COLOR]
    35. else if (serial_byte=='B')
    36. control_relay=4[COLOR=#ff0000][B];[/B][/COLOR]
    37. else if (serial_byte=='C')
    38. control_relay=7;
    39. else if (serial_byte=='D')
    40. control_relay=8;
    41. [COLOR=red][B]}[/B][/COLOR]
    42.  
    43. // We have the control byte, now lets look for 1 or 0
    44. if (serial_byte=='1')
    45. {
    46. // Turn specified relay on
    47. if (control_relay==2[COLOR=red][B])[/B][/COLOR]
    48. state_FilterPump=HIGH;
    49. if (control_relay==4[COLOR=red][B])[/B][/COLOR]
    50. state_BoosterPump=[COLOR=red][B]HIGH[/B][/COLOR];
    51. if (control_relay==7[COLOR=red][B])[/B][/COLOR]
    52. state_SpaBlower=HIGH;
    53. if (control_relay==8[COLOR=red][B])[/B][/COLOR]
    54. state_Lighting=HIGH;
    55. }
    56. else if (serial_byte=='0')
    57. {
    58. // Turn specified relay off
    59. if (control_relay==2)
    60. state_FilterPump=LOW;
    61. if (control_relay==4)
    62. state_BoosterPump=LOW;
    63. if (control_relay==7)
    64. state_SpaBlower=LOW;
    65. [B][COLOR=red]i[/COLOR][/B]f (control_relay==8)
    66. state_Lighting=LOW;
    67. }
    68. [COLOR=red][B]}[/B][/COLOR]
    69. [COLOR=red][B]}[/B][/COLOR]
    70.  
    71.  
    I have enclosed your code with code tags to make it easier to read.

    hgmjr
     
    Last edited: Apr 17, 2011
    aouate3 likes this.
  3. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    Thank you, it does make it easier to read
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I put in bold red the changes that were needed to get the code to compile with the Arduino 0022.

    That does not mean that it is bug free. That just got it to compile.

    hgmjr
     
  5. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
  6. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The #include line is incomplete. Normally it is followed by a filename with .h extension. There seems to be something missing.

    hgmjr
     
  7. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    ok, the http://www.atari-source.org/node/65 site, the text after the #include, and and before the void setup(), is it vital to the operation of this code? it seems to tell the starting serial byte, but is it possible to add that in somewhere else and eliminate the #include section all together?

    OR is the #include section supposed to be put into a seperate file? and that files filename be put into to the arduino code?
     
  8. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I believe what is intended there is the code line

    I assume this because the code invokes EEPROM.write and EEPROM.read instructions.


    hgmjr
     
  9. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    But i would need the eeprom.h file wouldnt i? :confused:
     
  10. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Diving into programming in C-language without studying the various C language constructs is going to be a bit painful for you. There are a number of tutorials on the Internet that will be useful for you to read.

    hgmjr
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214

    EEPROM.h is already in the Arduino 0022 package you loaded.

    Just add the line and it should compile just fine.

    hgmjr
     
  12. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    Oh Ok, Thanks
     
  13. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    IT COMPILED, i feel so accomplished.. yet it was probably really simple... well.. i am a noob after all..
     
  14. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Congratulations on your first success.

    As an beginner in the world of microcontrollers, you have chosen a good learning environment in the Arduino Uno.

    If you invest some time to familiarize yourself with C-language, you will be able to clearly appreciate what each of the code lines mean/does.

    Have you had any experience with programming in other languages perhaps?



    hgmjr
     
  15. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    In html mostly, a little visual basic, but not much, i can understand code, and manipulate it to a extent, but when it comes to writing it, i cant do it..

    html is the only language i can write efficiently
     
  16. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    For the Arduino Uno and its predecessor the Arduino Duemilanove, there are many example programs out there that you can study. The C-language is pretty easy to understand. The main thing to learn is that in C, like all programming languages, the syntax is important. The compiler will complain if you have even the slightest syntax error. The Arduino compiler gives you pretty clear hints as to what it has found. If you notice the numbers on each of the error messages, these are the line numbers in the source code where the error was found.

    hgmjr
     
  17. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    Ok, yea, im going to starting looking into C, and hopefully writing a few programs to get me familiar, i also noticed something, i didnt press the insert button on my keyboard, when i was editing the code, it was deleting characters.. that was one of the reasons why it didnt compile, the other reason was, of course, me being new to this, and i appreciate all the help you have provided.
     
  18. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    That is why the AAC forum exists. To assist members such as yourself in overcoming whatever is holding them up in their project.

    hgmjr
     
    aouate3 likes this.
  19. aouate3

    Thread Starter Member

    Apr 17, 2011
    91
    0
    i added the temperature code from http://pscmpf.blogspot.com/2008/12/arduino-lm35-sensor.html

    and everything compiled properly :D

    but i just wanted to check with you, and see if there will be any potential problems, interfacing wise, and also, how hard would it be to modify the processing program on that same site to relay info from two temperature sensors


    Here is my current code

    Code ( (Unknown Language)):
    1. #include "EEPROM.h"
    2. int state_FilterPump=0, state_BoosterPump=0, state_SpaBlower=0, state_Lighting=0;
    3. bool have_control=false; // Temp variable
    4. int control_relay=0; // Temp variable
    5.  
    6. //declare variables for temperature
    7. float tempEnvironment;
    8. int tempPin = 0;
    9.  
    10. float tempWater;
    11. int tempPin2 = 1;
    12.  
    13. void setup()
    14. {
    15. pinMode(2, OUTPUT); // sets the digital pin as output
    16. pinMode(4, OUTPUT); // sets the digital pin as output
    17. pinMode(7, OUTPUT); // sets the digital pin as output
    18. pinMode(8, OUTPUT); // sets the digital pin as output
    19.  
    20. // Prepare the serial ports
    21. Serial.begin(9600);
    22. Serial.flush();
    23.  
    24. }
    25. void loop()
    26. {
    27. int serial_byte=0;
    28.  
    29. if (Serial.available()>0)
    30. {
    31. /* Here we wait for the first byte of a two byte control sequence. Send A, B or C to control filterpump,boosterpump, light, or blower.
    32. */
    33.  
    34. serial_byte=Serial.read();
    35. if (!have_control)
    36. {
    37. have_control=true;
    38. if (serial_byte=='A')
    39. control_relay=2;
    40. else if (serial_byte=='B')
    41. control_relay=4;
    42. else if (serial_byte=='C')
    43. control_relay=7;
    44. else if (serial_byte=='D')
    45. control_relay=8;
    46. }
    47.  
    48. // We have the control byte, now lets look for 1 or 0
    49. if (serial_byte=='1')
    50. {
    51. // Turn specified relay on
    52. if (control_relay==2)
    53. state_FilterPump=HIGH;
    54. if (control_relay==4)
    55. state_BoosterPump=HIGH;
    56. if (control_relay==7)
    57. state_SpaBlower=HIGH;
    58. if (control_relay==8)
    59. state_Lighting=HIGH;
    60. }
    61. else if (serial_byte=='0')
    62. {
    63. // Turn specified relay off
    64. if (control_relay==2)
    65. state_FilterPump=LOW;
    66. if (control_relay==4)
    67. state_BoosterPump=LOW;
    68. if (control_relay==7)
    69. state_SpaBlower=LOW;
    70. if (control_relay==8)
    71. state_Lighting=LOW;
    72. }
    73. {
    74. tempEnvironment = analogRead(tempPin);           //read the value from the sensor
    75. tempEnvironment = (5.0 * tempEnvironment * 100.0)/1024.0;  //convert the analog data to temperature
    76. Serial.print((byte)tempEnvironment);             //send the data to the computer
    77. delay(1000);                           //wait one second before sending new data
    78.  
    79.  
    80. tempWater = analogRead(tempPin2);           //read the value from the sensor
    81. tempWater = (5.0 * tempWater * 100.0)/1024.0;  //convert the analog data to temperature
    82. Serial.print((byte)tempWater);             //send the data to the computer
    83. delay(1000);                           //wait one second before sending new data
    84. }
    85. }
    86. }
    87.  
     
  20. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I guess you recognize that the code at the blogspot.com link is intended to run on your PC. It receives temperature data over the serial link and displays it in graphical format.

    It is written and compiled by the IDE referred to as Processing. I have no experience in writing code using Processing. I do believe that it runs under JAVA. When it comes to Processing and Java I am out of my element.

    For additional details, I would suggest that you consider availing yourself of the assistance that you can obtain from the Arduino website forum at www.arduino.cc.

    hgmjr
     
Loading...