Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2010  |  syntax: C++  |  size: 4.17 KB  |  hits: 1,128  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. This code is for interfacing an arduino mega to the modules of my
  3. vehicle responsible for climate control. The blend door actuator is
  4. a motor which controls the blend of hot and cold air and has a built
  5. in potentiometer for a readout of it's position, and is attached to
  6. an adafruit motor shield. Four solenoids attached using the circuit from
  7. http://www.arduino.cc/playground/uploads/Learning/solenoid_driver.pdf
  8. control the vent selection. A pwm signal is amplified by a board in
  9. the car which controls the blower motor. Three 10K linear taper
  10. potentiometers set the desired positions for each module.
  11. */
  12.  
  13. #include <AFMotor.h>
  14.  
  15. AF_DCMotor motor(4);            //Blend Door Actuator
  16.  
  17. #define blowpwmPin 10           // digital pin [pwm] ouptutting control wave
  18.  
  19. #define sol1Pin 22              // digital pin connected to solenoid tier 1
  20. #define sol2Pin 24              // digital pin connected to solenoid tier 2
  21. #define sol3Pin 26              // digital pin connected to solenoid tier 3
  22. #define sol4Pin 28              // digital pin connected to solenoid tier 4
  23.  
  24. int tempVal;                    // variable to read the value from the temperature pot
  25. int ventVal;                    // variable to read the value from the vent pot
  26. int blowVal;                    // variable to read the value from the blower pot
  27. int bdaVal;                     // variable to read the value from the blend door pot
  28. int pwmVal;                     // variable to store the math of blower potentiometer to pwm
  29.  
  30. void setup()
  31. {
  32.         Serial.begin(9600);
  33.         pinMode(blowpwmPin, OUTPUT);            //Pins default to Inputs unless otherwise specified
  34.         pinMode(sol1Pin, OUTPUT);
  35.         pinMode(sol2Pin, OUTPUT);
  36.         pinMode(sol3Pin, OUTPUT);
  37.         pinMode(sol4Pin, OUTPUT);
  38.         tempVal = 0;                            //Making sure the analog readings get a fresh start
  39.         ventVal = 0;
  40.         bdaVal = 0;
  41.         blowVal = 0;
  42.         pwmVal = 0;
  43. }
  44.  
  45. void loop()
  46. {
  47.   tempVal = map(analogRead(3), 0, 1023, 1, 9);  //reads the analog pin, changes the value to a -
  48.   delay(50);                                    //more manageable one and stores it the delays -
  49.   bdaVal = map(analogRead(5), 0, 1023, 0, 10);  //are to give time for accurate readings
  50.   delay(50);
  51.   blowVal = map(analogRead(2), 0, 1023, 0, 10);
  52.   delay(50);
  53.   analogWrite(blowpwmPin, blowVal * 25.5);      //PWM controls the Blower motor speed
  54.   pwmVal = blowVal * 25.5;                      //storing the PWM value for recall
  55.   ventVal = map(analogRead(1), 0, 1023, 0, 5);
  56.   delay(50);
  57.   Serial.println("Temperature Setting:");       //if the pc is hooked up to the arduino -
  58.   Serial.println(tempVal);                      //with a serial terminal open, the values -
  59.   Serial.println("Blend door position:");       //of the sensors are displayed on screen -
  60.   Serial.println(bdaVal);                       //for debugging purposes by these next few lines
  61.   Serial.println("Vents Setting:");
  62.   Serial.println(ventVal);
  63.   Serial.println("Blower Motor Setting:");
  64.   Serial.println(blowVal);
  65.   Serial.println("Blower PWM Frequency:");
  66.         switch (ventVal) {                      //these next lines are the control of the vents
  67.         case 0:    // OFF
  68.         digitalWrite(sol1Pin, HIGH);
  69.         digitalWrite(sol2Pin, LOW);
  70.         digitalWrite(sol3Pin, HIGH);
  71.         digitalWrite(sol4Pin, HIGH);
  72.     break;
  73.         case 1:    // MAX A/C
  74.     digitalWrite(sol1Pin, HIGH);
  75.         digitalWrite(sol2Pin, HIGH);
  76.         digitalWrite(sol3Pin, HIGH);
  77.         digitalWrite(sol4Pin, HIGH);
  78.     break;
  79.         case 2:    // NORM A/C [PANEL]
  80.     digitalWrite(sol1Pin, LOW);
  81.         digitalWrite(sol2Pin, HIGH);
  82.         digitalWrite(sol3Pin, HIGH);
  83.         digitalWrite(sol4Pin, HIGH);
  84.     break;
  85.         case 3:    // FLOOR
  86.         digitalWrite(sol1Pin, LOW);
  87.         digitalWrite(sol2Pin, LOW);
  88.         digitalWrite(sol3Pin, HIGH);
  89.         digitalWrite(sol4Pin, HIGH);
  90.     break;
  91.         case 4:    // FLOOR/DEFROST
  92.     digitalWrite(sol1Pin, LOW);
  93.         digitalWrite(sol2Pin, LOW);
  94.         digitalWrite(sol3Pin, LOW);
  95.         digitalWrite(sol4Pin, HIGH);
  96.     break;
  97.         case 5:    // DEFROST
  98.     digitalWrite(sol1Pin, LOW);
  99.         digitalWrite(sol2Pin, LOW);
  100.         digitalWrite(sol3Pin, LOW);
  101.         digitalWrite(sol4Pin, LOW);
  102.     break;
  103.         }
  104. if (tempVal == bdaVal)                          //the following lines are a simple blend door PID
  105. {
  106.   motor.run(RELEASE);
  107. }
  108. else if (bdaVal > tempVal)
  109. {
  110.   motor.run(FORWARD);
  111.   motor.setSpeed(255);
  112.   delay(500);
  113. }
  114. else if (bdaVal < tempVal)
  115. {
  116.   motor.run(BACKWARD);
  117.   motor.setSpeed(255);
  118.   delay(500);
  119. }
  120. delay(1000);                            //a second, exactly, for everything to catch up
  121. }