controling digital output with analog input by using magnet and a coil

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

ali9:
so will this work ?

No. It won't even compile.

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.

i put 1.38v because that was the voltage that came from my pc through usb port to arduino without connecting the coil

No, that voltage was random noise pickup. Reading any input that has nothing attached to it is meaningless:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

PeterH:

ali9:
so will this work ?

No. It won't even compile.

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

but it did compile

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.

Why do the math to turn it into a float? Why not just use the 0-1023?

I'd use the internal reference. Depending on the Arduino (you don't say), most have a 1.1V internal reference.

You'll need to protect the Arduino input from negative voltage and voltages greater than 5V.

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.