int sensorValue = 0;
int outputValue = 0;
int transistorPin = 2;
int analogInPin = ?; // you must state which analog pin you are using
void setup() {
pinMode(transistorPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(analogInPin); // sensorValue = analogRead(analogInPin)/4;
// the map function will perform the /4, don't do it twice
analogWrite() commands only work on arduino pins that support PWM because internal timer support is needed and they are only 'hardwired' to specify arudino pin numbers, depending on what processor chip your board uses (Mega board has more timers then Uno board). The arduino reference page for analogWrite command tells which output pins will support PWM: http://arduino.cc/en/Reference/AnalogWrite
What is diode correct place? Have you a detailed example for a beginner?
Look at the below linked drawing of controlling a solenoid and see where the diode is wired and which direction the diode is to be wired, it's the same for a simple single winding DC motor: Arduino Playground - HomePage
Pins 3, 5, 6, 9, 10, and 11 can do PWM "analogWrite()". When you say:
analogWrite(3, 0); // Pin 3 is off all the time
analogWrite(3, 64); // Pin 3 is on 1/4 of the time and off 3/4 of the time.
analogWrite(3, 128); // Pin 3 is on half the time and off half the time.
analogWrite(3, 192); // Pin 3 is on 3/4 of the time and off 1/4 of the time.
analogWrite(3, 255); // Pin 3 is on all the time
The pulses are quite fast so for LED's and motors it acts like an analog signal.
If you use a pin that can't do PWM you get LOW for all values below 128 and HIGH for all values above 127. That is why your motor on Pin 2 is doing just OFF (LOW) and ON (HIGH).
Hi!
I have arduino UNO and Bridge H L298
Conect your DC Motor to Bridge H and conect in1 to PIN11 Arduino.
Then, PIN1 of the potenciometer to 5v, PIN2 to A0 and PIN3 to GND and here the programming
Good Luck! (Sorry my english, I'm from Argentina)
NOTE: YOU MUST CONECT THE GND ARDUINO TO GND BRIDGE H!
int pot = 0;
int motorPin = 11;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(pot, INPUT);
pinMode(motorPin, OUTPUT);
}
int getPot() {
int v;
v = analogRead(pot);
v /= 4;
v = max(v, 60);
v = min(v, 255);
return v;
}
int motorFoward() {
analogWrite(motorPin, getPot());
}
int motorBackward() {
analogWrite(motorPin, getPot());
}
void loop() // run over and over again
{
motorFoward();
motorBackward();
}