hey
i want to give HIGH TO DIGITALPIN 9 only if there was a voltage in analog pin A0 , in A0 i will connect a coil and when i pass the magnet through it it will generate voltage and when it does i want it to give digitalpin9 HIGH(5.0V) and it will stay HIGH UNTIL I REST THE ARDUINO here is the code i wrote :
int transistorPin = 9;
int sensorPin = A0;
int sensorValue = 0;
void setup(){
pinMode(transistorPin, OUTPUT);
}
void loop(){
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
while ( voltage > 1.38 ) ;{ i put 1.38v because that was the voltage that came from my pc through usb port to arduino without connecting the coil
digitalWrite(transistorPin, HIGH);
while ( voltage < 1.38 ); {
digitalWrite(transistorPin, HIGH);
}
delay(1000);
}
}
so will this work ? ... p.s : the maximum voltage i got from the coil and magnet was 0.15v
Neither of your while loops are valid, and I can't see any purpose for them anyway. You want to set an output high if the input value is greater than a threshold. In other words: if the input value is greater than the threshold, set the output HIGH. Translating that from pseudocode into C++ should be trivial for you.
Connecting an inductive input to an I/O pin is quite dangerous since any negative voltage and any positive voltage over 5V will damage the Arduino. Your inductive coil could easily generate a negative voltage and the positive voltage could increase substantially depending on the speed and position of the magnet, so there is a very real danger that you will destroy the Arduino. If you want to do this properly, you would need a conditioning circuit between the coil and the Arduino to keep the applied voltage between 0V and 5V. That would also beg the question of whether a magnet/coil is the most appropriate sensor for whatever it is you're trying to measure.
Neither of your while loops are valid, and I can't see any purpose for them anyway. You want to set an output high if the input value is greater than a threshold. In other words: if the input value is greater than the threshold, set the output HIGH. Translating that from pseudocode into C++ should be trivial for you.
Connecting an inductive input to an I/O pin is quite dangerous since any negative voltage and any positive voltage over 5V will damage the Arduino. Your inductive coil could easily generate a negative voltage and the positive voltage could increase substantially depending on the speed and position of the magnet, so there is a very real danger that you will destroy the Arduino. If you want to do this properly, you would need a conditioning circuit between the coil and the Arduino to keep the applied voltage between 0V and 5V. That would also beg the question of whether a magnet/coil is the most appropriate sensor for whatever it is you're trying to measure.
thanks for helping me but it did compile and i want to know how much threshold value is?
, thanks in advance
No the code you posted will not compile. You might have got something to compile but it was not the code you posted. Do a copy an paste into the arduino and see.
i want to know how much threshold value is?
No idea until you try it because this depends on the number of turns, the strength of the magnet and the speed you put it in an out of the coil.
Do that and measure the voltage you get by outputting the value from the serial read to the terminal.
Make sure to add those protection diodes before you try it.
The major problem with that code is the lack of a // I front of the comment, that stops it from compiling, and the use of the semicolon ; after each of the two while statements, that stops those loops from working.