hi boys and girls,
i want to build an cleaning robot and im in my studies how this is going to work. I want to power a
dc Motor and let him turn the direktion if i switch a button. this will later be an distance sensor.
so my idea was to read out a voltage with the analog P0 and if its reading current the motor changes direktion. I tested reading the voltage and everything works fine but if I try it out with my Motor programm I cant read out the current.
thats the code in which it dont work:
int mRechts = 12;
int mLinks = 13;
int mRbremse = 9;
int mLbremse = 8;
int geschwindigkeit;
int sensorValue = analogRead(A0);
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
these are the same lines of code in the both programms but just one works
pls help
and sry für my bad english ^^
EdenCh
What's your schematic? "Reading current" is actually "reading voltage" using Ohm's law, V = IR. You measure the current by measuring voltage over a "shunt" resistor with very low resistance (so that it doesn't affect current flow much). If your schematic is wrong, it won't work.
I have a closed circuit with 3.3V into an LED ( which is the resitor you are talking about) into GND.
i measure the voltage at the in Pin of the LED into A0 so it has to give me 3.3V in the serial monitor which it does in the second code. there are no problems. but in the first code (with the same circiut)
i cant read the 3.3V out of the serial monitor and its the same code :/. thats what i dont understand.
@EdenChemiker, you seem to have posted 1 program and part of another. Post both programs in full and also post a diagram showing how you have everything connected.
And when posting code please use the code button </>so your code looks like thisand is easy to copy to a text editor See How to use the Forum
My whole Plan ist that i want to build an Distanzen Sensor wich controlles the motor. So if something ist nearby the motor spins backwards. I dont have the Sensor yet so im trying to Imitate it with the LED circut. So if in get an Signal (the A0 Pin reads voltage) the motor spins backwards. I Tested olny the LED circut and i could read the voltage in the serial Monitor. That works Just fine. But if i want to use the motor and the code for the Motor with this led circut (its the same code as i Tested it bevore) it shows nothing in the serial Monitor. I dont understand why its the same thing is there a better solution ?or do i miss something?
int distance = analogRead(A0); // Ultrasonic sensor test value
if (distance < 1234) { // This will be found out when you actually have the sensor
motor.driveBackwards();
}
I'm still not sure what you're trying to do with the LED since I don't have an actual schematic.