Hey everyone! i just got started using the arduino. I have been messing around with electronics for a while, but never understood them until now. I also have basic programming skills. Anyways, heres what I'm doing. I have a (very)small dc motor, that I'm controlling using an npn transistor and an external power source of 3v. Then I'm reading values off of a photoresistor, mapping them to values between 0 and 255, then writing that to the base of the transistor. the problem is it works for the most part, but kinda goes wacko if you totally cover it, and sometimes it just does it randomly. heres my circuit.
If there's a problem with your code, I'm not seeing it.
Have you tried hard-coding PWM values (not using the light sensor) to see how the motor responds to various PWM values? The motor might not behave as you expect, especially if there is no mechanical load, or if the load isn't constant.
When you un-comment the Serial.println line, do you get the values you are expecting?
You might add another Serial.println to look at the PWM values too. Maybe that will give you a clue.
Thank you for your help. Beforehand I wrote down the minimum and maximum values of the photo resistor. But I think I used a different resistor value. Anyways I found out that the new values are 350 and 50. I did write my own constrain code, because when covering the photo resistor the motor stops, and its supposed to go its fastest(at least in my new code). I found that using a couple if statements like so, if sensorValue > 350 sensorValue = 350; that it worked fine. So long story short I got it working. Edit, I forgot that constrain returns the constrained value, and doesn't automatically set it. It should be sensorData = constrain(...);
Anyways I have a new problem. I had an old lm317 voltage regulator laying around, and I needed a 5v regulator(from a 9v) for some servos. So I calculated the values for r1 to be 300 ohms and r2 to be 1k ohms. So I set it all up with some caps, and it didn't work. The thing heated up like crazy, and no voltage was outputted. I did realize a second later that I had the electrolytic cap backwards and quickly flipped it. Did I bust it? It still does the same thing, heats up like crazy and outputs nothing. I double checked my circuit a couple times, making sure I had the right things on the right pins for the VR, as the schematic is not in the same layout as the actual VR. What do you think the problem is?