I am trying to make upa code where I can control a small motor with chip that act as a mosfet that I got from ebay.
I got this code to control one int in one way with a pot.
const int transistorPin = 9; // connected to the base of the transistor
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
// read the potentiometer:
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
}
That one work rather well, but when I tried to make the next one.
float pressLength_milliSeconds =0;
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;
int transistorPin0 = 9; // connected to the base of the transistor
int transistorPin1 = 8;
int buttonPin = 2;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
// set the transistor pin as output:
pinMode(transistorPin0, OUTPUT);
pinMode(transistorPin1, OUTPUT);
Serial.begin (9600);
}
void loop() {
while (digitalRead(buttonPin) ==LOW){
delay(100);
pressLength_milliSeconds = pressLength_milliSeconds + 100;
Serial.print("ms = ");
Serial.println(pressLength_milliSeconds);
}
if (pressLength_milliSeconds >= optionTwo_milliSeconds){
// read the potentiometer:
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin0, outputValue);
}
else if(pressLength_milliSeconds >= optionOne_milliSeconds){
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin1, outputValue);
}
pressLength_milliSeconds = 0;
}
It compiles flawlessly, however it does nothing in action. Can some one point out what I did wrong and help me adjust the code. :o ![]()