The code is after the break....
Turning Cat Code:
// Based on// File > Examples > Servo > Knob// Controlling a servo position using a// potentiometer (variable resistor)// by Michal Rinott ////also based on Fade, Analog Input (David Cuartielles, modified by Tom Igoe), and AnalogInOutSerial (Tom Igoe)//April 30, 2012#include <Servo.h>Servo myservo; // create servo object to// control a servoint sensorPin = 0; // analog pin used to// connect the// potentiometerint val; // variable to read the//value from the analog pinint lightPin = A2; // select the input pin for the potentiometerint ledPin = 11; // select the pin for the LEDint sensorValue = 0; // variable to store the value coming from the sensorint outputValue = 0; //value output to the PWM (analog out)void setup(){// declare the ledPin as an OUTPUT:pinMode(ledPin, OUTPUT);Serial.begin(9600);myservo.attach(9);// attaches the servo// servo on pin 9 to the servo objectpinMode(0, INPUT); //analog pin 0 is an input}void loop(){val = analogRead(sensorPin);// reads the value// of the potentiometer//(value between 0 and 900)Serial.println(outputValue);val = map(val, 0, 1023, 0, 179);// scale it to use it with the servo// (value between 0 and 180)myservo.write(val); // sets the servo// according to the// scaled valuedelay(15); //waits for the servo// to get there// read the value from the sensor:int sensorValue = analogRead(lightPin);outputValue = map(sensorValue, 60, 700, 0, 255);// turn the ledPin onanalogWrite(ledPin, outputValue);// stop the program for <sensorValue> milliseconds:delay(10);}
No comments:
Post a Comment