How to build a two-direction motor controller. For faster motors, use N-channel MOSFETS. Otherwise NPN transistors are fine. Here is the code:
int mSpeed = 0;
int stage;
void setup() { pinMode(9, OUTPUT);
pinMode(8, OUTPUT); mSpeed = 0;
stage = 1;
}
void loop() { switch (stage){
case 2: mSpeed--; analogWrite(9, mSpeed); if(mSpeed == 0){ stage = 3; } break;
case 3: mSpeed++; analogWrite(8, mSpeed); if(mSpeed == 255){ stage = 4; } break;
case 4: mSpeed--; analogWrite(8, mSpeed); if(mSpeed == 0){ stage = 1; } break;
case 1: mSpeed ++ ; analogWrite(9, mSpeed); if(mSpeed == 255){ stage = 2; } break; }
delay(mSpeed / 10);
}
i been trying to understand transistor i have bought a book and online search but i just dont get it gow they work and what they do, can you please help me
supersanchez1991 10 months ago
@supersanchez1991 thanks for the comment. I was actually planning on doing a video tutorial on how to interface transistors with microcontrollers. Since you asked, I will place additional emphasis on the transistor circuitry. I should have time to do it this week. Please subscribe for updates.
Bizorke 10 months ago