My project uses a potentiometer to change the speed of the motor. On a different pin, a solenoid is just opening and closing.
int MotPin = 9; // sets base of motor transistor to PWM 9
int potPin = 0; // sets potentiometer to analog 0
int potValue = 0; // value returned from the potentiometer
int SolPin = 1; // sets base of solenoid transistor to digital 1
int switchPin = 3; // sets switch pin to digital 3
int switchStatus; // status of the switch
void setup() {
pinMode(MotPin, OUTPUT); // sets motor transistor pin as output
pinMode(potPin, INPUT); // sets potentiometer pin as input
pinMode(SolPin, OUTPUT); // sets solenoid transistor pin as output
pinMode(switchPin, INPUT); // sets switch pin as input
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin) / 4; // read potentiometer, convert to 0 - 255
analogWrite(9, potValue); // use potentiometer to control transistor
switchStatus = digitalRead (switchPin); // reads status of switch
if (switchStatus == HIGH) { // If switch is pressed...
digitalWrite(SolPin, HIGH); // turn solenoid on
Serial.println("button pressed");
delay(100);
}
else { // otherwise ...
digitalWrite(SolPin, LOW); // turn solenoid off
Serial.println("button not pressed");
delay(100);
}
}
We’d like to see a circuit schematic, a photo of the project, and perhaps a video of it as well.
Posting the code alone doesn’t really tell us enough.
Comment by mdgross — February 13, 2008 @ 7:07 am |