The code for this is a mix between code for CIRC04: Servo and CIRC10: Temperature. What we're doing is attaching the value for the temperature, to a value of the servo. Let's walk through some parts of it:

Inside, the loop, we're going to read in a voltage from the pin using the getVoltage() function from CIRC04. We are then going to pass this value to a new function, convertToF(), which is generated from the voltage value, to convert the voltage to a degrees Fahrenheit temperature:

Download: file
  // read the voltage from the pin
  float voltage = getVoltage(temperaturePin);
  // convert the voltage to a temperature value
  float temperature = convertToF(voltage);

Then, we're going to constrain the temperature values. This is totally up to you, you can use any number, but we are using -10F and 100F as our minimum and maximum temperature accepted by the TMP36:

map((int(temperature)), -10, 100

We still need to map the temperature values to servo values. The minimum degrees a servo can move is 0 degrees and the maximum degrees is 180, so let's set the servo to map: -10 to 0 and 100 to 180:

Download: file
servoPos = map((int(temperature)), -10, 100, 0, 180);

Then, write servoPos to the servo!

Download: file
// write servoPos to the servo
 metroServo.write(servoPos);
 // poll every 0.5sec
 delay(500);

Here's the full code, with all the helpers built in:

Code

/* 
 *  PROJ07 - RGB Color Mixer
 *  
 *  by Brent Rubell for Adafruit Industries
 */

// RGB LED Pins
int rgbLED[] = {9, 10, 11};

// trim potentiometer pin
int trimPin = A0;
// button pin 
const int buttonPin = 12;

// button state
int buttonState = 0;
// trim pot. value
int trimValue = 0;


int colorIdx = 0;
int red = 0;
int green = 0;
int blue = 0;

boolean CURRENTRGB[] = {0, 0, 0};

void setup() {
  // Setup Serial
  Serial.begin(9600);
  // set the 3 pins as output pins
  for(int i = 0; i < 3; i++) {
    pinMode(rgbLED[i], OUTPUT);
  }
  // initialize the push-button as an input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the value of the push-button 
  buttonState = digitalRead(buttonPin);
  
  if(buttonState == LOW) {
    delay(2);
    // reset the colorIdx if it goes past Blue (colorIdx = 3)
    if(colorIdx == 3) {
      colorIdx = 0;
    }
    colorIdx++;
    switch(colorIdx) {
      case 1:
        trimValue = analogRead(trimPin);
        red = map(trimValue, 0, 670, 0, 255);
        CURRENTRGB[0] = red;
        break;
      case 2: 
        trimValue = analogRead(trimPin);
        green = map(trimValue, 0, 670, 0, 255);
        CURRENTRGB[1] = green;
        break;
      case 3: 
        trimValue = analogRead(trimPin);
        blue = map(trimValue, 0, 670, 0, 255);
        CURRENTRGB[2] = blue;
        break;
      default:
        break;
    }

    Serial.println("red:");
    Serial.print(CURRENTRGB[0]);
    Serial.println(" ");

    Serial.println("green:");
    Serial.print(CURRENTRGB[1]);
    Serial.println(" ");

    Serial.println("blue:");
    Serial.print(CURRENTRGB[2]);
    Serial.println(" ");
  
    setColor(rgbLED, CURRENTRGB);
    delay(1000);

    
  }
  
}

void setColor(int* led, const boolean* color) {
  for(int i = 0; i < 3; i++){
    digitalWrite(led[i], color[i]);
  }
}
This guide was first published on Aug 18, 2017. It was last updated on Aug 18, 2017. This page (Code) was last updated on Mar 06, 2020.