// Create variables for the pins we'll use:
int sensorPin = A2;
int sensorValue = 0;
int redLED = 6;
int blueLED = A8;
void setup()
{
// Initialize the sensor pin as an input, but without a pullup
// (Pullups are only used for switch inputs)
pinMode(sensorPin, INPUT);
// Initialize the output pins:
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
// Initialize the serial monitor:
Serial.begin(9600);
}
void loop()
{
int sensorValue;
// Read the sensor value (will be 0 to 1023):
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue,0,1023,0,255);
// Print out the sensor reading to the serial monitor:
Serial.print("sensor value: ");
Serial.println(sensorValue);
// Since the sensor value is 0 to 1023,
// and analogWrite needs a value from 0 to 255,
// we'll divide the sensor value by four to scale it down:
analogWrite(redLED,sensorValue -255 / 2);
analogWrite(blueLED,sensorValue -255 / 2);
delay(1000);
}
Comments
Please log in or sign up to comment.