7 segment reed switch speedometer help

Hi,

I am kind of new to programming here and am working on creating a seven segment display speedometer with a reed switch. So far I have successfully connected the seven segment displays and run them.
I know the reed switch is registering the magnet since the led I attached to the other end lights up when i pass a magnet.

The problem is when I upload my code to the Arduino Mega it keeps freezing and wont return any values. Below is my code.

int i = 0;
int rps = 0;
void setup(){ 
pinMode(0, INPUT); 
Serial.begin(9600);
}
void reed(){  //determines if magnet is passing by the reedswitch
  if (analogRead(0) >= 400){  //value is 400 because LED 
  rps = rps + 1;              //is drawing power
  delay(50);            //delay so the arduino doesnt double count
  i + 1;
  chk();  
  }
  else {
  rps = rps + 0;    //tried to replicate above so time is the same
  delay(50); 
  i + 1; 
  chk(); 
  }
  }  
void chk(){    //determines if the wait for values is over
  if (i < 500){
  reed();
 }
  else{
    Serial.println(i);  //writes the rounds per 500 cycles
    loop();
  }
}
void loop(){  
geschchk(); 
}
void geschchk(){  //resets values and starts reed program
i = 0;
rps = 0;
reed();
}

I tried using the millis(); function earlier, but my Arduino froze after printing 2 values.

unsigned long time;
int rps = 0;
void setup(){ 
pinMode(0, INPUT); 
  Serial.begin(9600);
}
void reed(){ 
  if (analogRead(0) >= 400){  //determies if magnet is present
  rps = rps + 1;              //used 400 instesd of 1023 becuase of LED
  delay(50); //prevents double counting
  chk();  
  }
  else{
  chk();
}
  }  
void chk(){    //determies if time has passed
  if (millis() < time + 500){
    reed();
  }
  else loop();
}
void loop(){
  Serial.println(rps);  //prints rounds per half a second
  geschchk(); 
}
void geschchk(){   //stores values
time = millis();  
rps = 0;
  reed();
}

Thanks in advance, any help would be appreciated

I almost forgot, I also had another version which worked with the millis() function, but it always returned different values and was very laggy. here is a link to the code running : http://ubuntuone.com/p/DHr/

and a link to a video of my project using the above linked code :
http://ubuntuone.com/p/DHu/

Thanks again.

For the counter use

void loop() {
   while (analogRead(0) >= 400){}                 // Wait for rising edge
   rps = rps + 1;     
   while(analogRead(0) < 400) {}                     // wait till falling edge
}

Thank you so much Marsheng, with the code you gave me, I was able to finish my speedometer, now I just have to implement the calculator to change the rounds per 500ms to km/hr, but that should be easy.

In case anyone is wondering, below is a link to my working code. FYI the 7 Segments I was using were common anode, so if you are using common cathode 7 segments, then you will have to change all the HIGH values to LOW and vice versa. Feel free to use my code even though it may not be the best.

http://ubuntuone.com/p/DKQ/

The speedometer is not accurate when going slow ex. less than 1 round per half-second. if you wish, you could add a second magnet to your wheel if you are making this for a bike, or increase the wait time to one or two seconds, but then the refresh rate will be lower. If this is for a car, i think it is fine the way it is.

I will post an updated version of this code with a button to change from km/h to mph (pushbutton).

For the speed, check the time between pulses

   while(analogRead(0) < 200) {}                  // Wait for rising edge
   startTime=micros();  
   rps = rps + 1;    
   Duration=startTime-lastTime;
   lastTime=startTime;
   Serial.print(rps);      
   Serial.print("  ");     
   Serial.print(ConversionFactor/Duration);          // Your signal will be slow enough to send during pulses. 
   Serial.print("  ");     
   while(analogRead(0) >= 200) {}                      // wait till falling edge. 
}

Thank you again for the suggestion on how to determine the speed, but I think I will just have the seven segments display the rps and drive at a constant speed of 45mph and then find the conversion factor.

Unfortunately, when I was driving today, the 7 segments were displaying random numbers ranging from 7 to 0 to 188. Later I disconnected the seven segment displays and just wrote the following code to test if the reedswitch was even registering.

void setup(){
pinMode(0, INPUT);
Serial.begin(9600);
}

void loop(){
  Serial.println(analogRead(0));
  }

This only returned random values around 463 whether or not the magnet was near the reedswitch (no resistor attached, so it should have been 1023). Even when I ran a wire directly from the 5v to analog in 0 pin, the values would not change, they just floated around 400 - 500. Meanwhile the serial monitor was still updating. Any ideas?

Afterward I re-uploaded the code and it worked, but when I installed the arduino in the car again, the same thing happened again with the random analogRead values. Sometimes it works, and sometimes it doesn't. Is something in my arduino broken?

here is the code I have right now. http://ubuntuone.com/p/DZA/

Here is a video of the insalled seven segment display while driving.
http://ubuntuone.com/p/DZF/

I was driving and it registered a pass since it was displaying a one, but when I reset it, it wouldn't register anything. (sorry about the Blair Whitch project style filming, I had to shift). I believe I was using the statement while (analogRead(0) >= 900 ...... If I use 600 it might work now, but when I turn the arduino off and back on again, it might be hovering around 600 again and not work anymore.

I'm at work and will look at the rest later but

void loop(){
Serial.println(analogRead(0));
}

will be sending every cycle. Too fast for the comms. Add delay(100) or and then see what happens.

My analog also hiccuped some time back until I added

analogReference(DEFAULT);

in the setup.

Thank you for the help again,

I added

analogReference(DEFAULT);

to the setup and it sort of helped. The thing is that I believe the program is adding 1 to rps more than once per cycle, but I cant figure out why.

void reed(){ 
  while (analogRead(0) >= 1000){}  //Checked ob ein magnet orbei ist. determies if magnet is present
  rps = rps + 1;   
  while(analogRead(0) < 1000){}
  chk();  
  }

Doesnt the above code prevent it from double counting?

When I just drove it gave me ridiculous values of how many times the magnet passed in one second. It said it passed like 70 times when I was driving a mere 10 miles per hour. On top of that it sometimes seems to be working, but then randomly spikes to high numbers.

I tried recording the analogRead values with the analogReference(DEFAULT) and it was hovering around 900 and 1023.

Thank you again for helping out so much.

Below is a printout of the values it hovers at while driving.

987
987
986
985
986
984
984
985
983
982
1023
1023
1023
1023
1022
1022
1021
1020
1018
1018
1016
1016
1015
1015
1014
1014
1012
1010
1012
1009
1009
1008
1007
1007
1007
1006
1005
1004
1003
1003
1002
1001
1001
1000
1000
998
997
998
998
997
996
996
995
995
994
993
992
992
992
991
991
990
990
990
990
989
988
987
987
987
987
986
985
985
984
984
983
982
981
981
981
981
980
1023
1023
1023
1023
1022
1022
1020
1018
1017
1017
1017
1016
1015
1015
1014
1014
1013
1012
1011
1010
1010
1010
1009
1009
1008
1007
1007
1006
1005
1005
1005
1003
1002
1003
1002
1002
1001
1000
1001
998
999
998
997
997
997
996
997
995
995
993
994
993
992
993
991
991
990
991
990
988
987
987
987
987
987
986
986
985
985
985
985
983
1023
1023
1023
1023
1022
1023
1021
1020
1019
1018
1017
1017
1015
1014
1014
1013
1012
1012
1010
1010
1009
1007
1008
1008
1008
1008
1006
1005
1005
1004
1003
1002
997
1001
1001
1000
1000
999
999
999
998
997
997
997
996
995
995
994
993
994
993
993
992
992
991
990
990
989
988
988
989
987
987
987
985
985
986
985
983
984
983
982
983
981
1023
1023
1023
1023
1023
1022
1022
1021
1020
1019
1019
1017
1018
1016
1016
1015
1013
1015
1013
1013
1012
1011
1011
1010
1009
1009
1008
1007
1007
1005
1005
1004
1003
1003
1003
1002
1002
1002
1000
999
998
998
998
997
998
996
996
994
995
994
994
994
993
992
991
990
990
989
990
988
988
988
988
987
986
987
986
985
984
983
983
983
982
1023
1023
1023
1023
1023
1022
1021
1020
1020
1019
1018
1017
1015
1015
1014
1014
1013
1013
1012
1011
1011
1010
1009
1008
1008
1008
1007
1006
1005
1006
1004
1003
1003
1002
1002
1002
1001
1000
1000
999
999
999
998
998
997
997
996
995
994
994
994
993
994
993
992
992
991
991
990
990
989
988
989
987
988
987
987
986
985
985
984
983
983
982
1023
1023
1023
1023
1022
1021
1021
1019
1019
1018
1017
1016
1016
1014
1014
1012
1011
1011
1011
1011
1011
1009
1009
1008
1007
1007
1005
1004
1004
1003
1004
1004
1002
1002
1001
1002
1001
999
998
997
997
996
996
996
996
996
995
995
994
994
992
992
991
991
990
991
990
991
989
990
989
988
987
986
986
985
985
985
985
983
984
983
983
981
1023
1023
1023
1023
1022
1022
1020
1019
1018
1017
1017
1016
1016
1015
1015
1014
1013
1012
1012
1011
1010
1010
1009
1008
1007
1007
1007
1006
1005
1006
1005
1005
1003
1004
1003
1002
1002
1000
1000
999
998
997
996
997
996
996
995
997
996
996
995
995
993
993
992
991
990
990
989
990
989
989
987
988
989
988
987
986
985
984
984
983
983
983
1023
1023
1023
1023
1023
1023
1021
1022
1021
1020
1019
1018
1018
1015
1015
1014
1014
1013
1013
1012
1011
1010
1009
1008
1008
1007
1006
1006
1005
1004
1003
1003
1002
1001
1001
1000
1000
1000
999
998
998
998
996
996
995
995
993
994
994
993
993
993
991
991
990
988
989
988
988
989
989
987
988
990
989
987
987
985
984
984
985
983
983
982
982
982
1023
1023
1023
1023
1021
1021
1021
1021
1019
1018
1017
1016
1015
1015
1015
1015
1014
1012
1012
1011
1009
1009
1008
1008
1008
1007
1007
1006
1005
1004
1004
1001
1002
1001
1001
1001
1000
1000
997
998
996
996
997
997
995
996
994
993
993
991
991
991
991
990
989
990
989
988
987
987
986
986
986
986
986
984
984
984
983
984
982
982
982
981
980
979
980
1023
1023
1023
1023
1023
1023
1021
1020
1020
1019
1018
1018
1018
1017
1015
1014
1014
1013
1013
1012
1011
1011
1011
1010
1010
1009
1009
1008
1006
1005
1004
1004
1003
1002
1001
1000
998
998
997
995
995
994
995
994
993
992
991
991
991
991
989
988
987
987
986
986
985
985
985
985
986
986
985
984
984
983
982
981
981
981
980
981
980
979
979
979
978
978
977
1023
1023
1023
1023
1023
1023
1021
1021
1020
1020
1020
1019
1017
1017
1016
1016
1015
1015
1015
1014
1013
1011
1010
1009
1008
1007
1006
1007
1007
1006
1006
1004
1004
1002
1002
1002
1002
1001
1000
999
999
998
998
997
996
995
996
995
994
993
993
993
992
992
992
991
991
992
991
990
990
989
988
987
986
986
985
985
984
984
983
983
983
983
982
982
982
981
981
1023
1023
1023
1023
1023
1023
1022
1022
1021
1020
1019
1018
1016
1017
1017
1016
1015
1015
1014
1014
1014
1013
1013
1012
1012
1011
1011
1009
1009
1009
1007
1007
1008
1007
1006
1006
1005
1004
1003
1002
1001
1002
1001
1001
1001
1001
1001
999
998
998
998
998
996
996
996
996
995
994
993
992
991
992
992
991
991
990
990
989
989
988
987
986
987
986
985
985
985
985
985
985
1023
1023
1023
1023
1023
1023
1023
1020
1020
1019
1017
1016
1016
1015
1013
1012
1011
1010
1009
1010
1009
1007
1006
1005
1005
1005
1004
1003
1002
1004
1001
1001
1000
999
999
998
996
996
996
997
996
995
993
993
992
992
991
991
990
989
990
990
989
989
990
989
988
987
987
987
988
988
987
987
986
985
985
984
984
983
983
983
982
982
982
981
980
980
980
979
977
977
976
976
976
977
978
977
1023
1023
1023
1023
1023
1022
1022
1021
1021
1019
1018
1017
1016
1015
1014
1014
1013
1012
1012
1011
1011
1010
1010
1010
1009
1008
1007
1007
1007
1006
1006
1005
1005
1004
1003
1002
1002
1001
1001
1000
999
999
998
998
997
997
996
996
995
994
994
994
994
993
993
993
992
991
990
989
989
987
987
987
987
987
986
985
984
984
982
981
981
981
979
979
979
977
977
976
977
976
976
976
975
976
975
974
974
974
973
973
973
974
1023
1023
1023
1023
1023
1022
1022
1021
1021
1019
1019
1018
1016
1015
1016
1016
1016
1016
1015
1015
1014
1013
1012
1011
1011
1010
1010
1009
1009
1008
1007
1007
1006
1006
1005
1005
1005
1004
1004
1003
1002
1001
1001
1001
1000
1001
999
999
999
999
998
997
998
998
996
996
995
995
995
994
993
993
993
993
993
991
990
990
990
991
989
988
988
988
987
987
987
985
985
985
984
984
983
984
983
983
983
981
981
981
981
980
980
979
979
978
978
977
977
1023
1023
1023
1023
1023
1022
1022
1021
1020
1019
1018
1018
1017
1016
1015
1015
1014
1013
1013
1012
1011
1010
1010
1009
1009
1007
1007
1007
1005
1005
1004
1004
1003
1002
1002
1001
1001
1000
1000
999
999
999
999
998
998
997
997
997
996
995
993
994
993
993
993
992
992
991
991
990
990
990
989
989
988
987
987
987
987
986
986
985
986
985
985
984
983
983
982
982
983
982
981
981
980
980
979
979
979
979
978
978
977
977
977
976
975
975
975
974
974
974
974
973
972
972
971
971
1023
1023
1023
1023
1023
1023
1022
1021
1020
1019
1019
1018
1018
1017
1016
1015
1014
1013
1013
1012
1011
1011
1010
1010
1010
1009
1008
1008
1006
1006
1006
1005
1005
1004
1003
1003
1002
1001
1001
1000
1000
1000
998
998
998
998
997
997
996
996
996
994
993
994
993
992
993
992
992
990
990
989
989
988
988
987
987
988
988
987
986
987
986
985
984
984
983
983
983
982
981
981
981
981
981
980
979
979
979
978
978
977
977
976
976
975
975
975
974
974
972
973
973
972
971
971
971
971
970
969
969
969
969
968
967
967
967
968
967
965
966
1023
1023
1023
1023
1023
1023
1023
1022
1021
1020
1019
1019
1017
1017
1016
1015
1016
1014
1013
1012
1012
1010
1010
1009
1009
1009
1009
1009
1008
1007
1006
1006
1005
1004
1004
1003
1003
1002
1002
1001
1001
1000
999
998
998
999
998
997
996
995
997
995
994
994
994
993
992
992
992
991
990
990
989
990
989
988
988
992
988
987
986
986
985
985
985
985
984
984
983
982
982
982
981
981
981
980
980
979
979
978
978
977
977
977
977
977
976
975
975
975
975
974
974
972
972
972
972
971
971
971
970
970
969
969
968
968
968
967
967
968
967
967
967
965
965
964
964
963
963
964
963
963
962
961
961
961
960
961
961
960
959
959
958
958
958
957
957
957
957
956
956
955
955
1023
1023
1023
1023
1023
1023
1023
1023
1022
1022
1021
1020
1019
1018
1018
1016
1016
1015
1015
1014
1013
1013
1012
1011
1010
1011
1010
1009
1009
1007
1008
1007
1006
1005
1005
1004
1004
1004
1002
1002
1001
1001
1000
1000
999
998
997
997
997
997
996
995
995
994
994
994
993
993
992
992
991
991
991
991
990
989
989
988
988
987
987
986
986
986
986
985
984
984
984
983
982
982
981
981
981
980
981
979
979
979
978
978
977
977
977
976
977
976
975
976
975
974
974
973
972
972
972
971
971
970
970
970
970
970
969
968
968
967
967
967
966
966
965
965
965
965
964
964
963
964
963
963
962
961
962
961
961
960
960
959
958
959
959
958
958
957
957
956
957
956
956
955
955
954
954
954
953
954
952
953
952
952
951
951
950
950
950
949
949
949
948
948
948
948
947
946
946
946
946
946
945
944
944
944
943
943
944
943
942
942
942
941
941
941
940
939
939
939
940
939
938
937
937
937
937
936
936
936
936
935
935
935
934
934
933
933
933
933
932
932
932
931
931
931
931
930
930
930
929
929
929
928
928
927
928
927
927
926
926
926
927
925
925
925
924
923
923
924
923
922
922
923
922
921
921
921
920
920
920
919
919
919
919
918
918
917
917
917
917
916
916
916
915
916
915
914
914
914
914
913
914
914
912
912
912
912
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1022
1021
1021
1020
1020
1018
1017
1016
1016
1015
1015
1014
1013
1012
1011
1011
1010
1010
1009
1008
1008
1007
1006
1006
1005
1005
1004
1003
1003
1002
1001
1001
1000
1000
999
999

Are you using a reed switch for the pickup ?

If a magnet and coil, it looks like you are not switching from 0 to 5 volts, but, only from something like 4-5 volts.

I bought a magnetic reed switch, but i cant remember where i bought it from. I thought it was from radioshack or youdoitelectronics.

If you are using a reed switch, you should be switching 0- 5 volts and reading with a digital input.

Be careful of engine noises etc. I had a bicycle speedo once that was triggered when I went over the loops at traffic lights.

Are you pulling the input low or high with a resistor when the switch is open ?

I am pulling the input low when the switch is open without a resistor. But you may be right, the values could be so strange due to the noise. I do have the arduino under the driver side dash where all the electronics are. Maybe the electronics are interfering with the arduino or reed switch cable. When I last tested it without any problems, I had the arduino in the passenger glove compartment. Ill try relocating it and see what happens.

As for the engine noise, I have placed the reed switch on the rear right tire, so I dont believe that should pose any problems.

The reason why I am using analogRead instead of digitalRead is because I couldnt get the digitalRead to work correctly. Otherwise I would have used the pulseIn function which would have been so much easier. Theoretically do you think the pulseIn function could work even though the reed switch is changing from HIGH to LOW so fast?

Isn't this just a contact bounce problem, or have you eliminated that possibility?

I have no idea, that might be a possibility, but it was working before, so I dont think that is the case here. If I were to find out if that were the case, how would I go about doing that?

I just ran a jumper cable from the 5v to the analog input 0 and when I connect the two, it goes to 1023, but when I disconnect them, it slowly counts down until it reaches a smaller number. This is with a 50ms delay. Is this normal?

1023
1023
1023
1023
1023
1023
1023
1023
1023
1010
976
971
969
948
938
945
942
920
915
919
917
894
894
889
881
876
867
866
869
861
848
848
844
846
827
830
833
832
806
801
811
804
792
786
798
791
776
778
779
780
759
754
765
764
745
740
753
753
737
734
741
738
721
716
725
725
709
710
711
713
703
702
707
707
697
694
691
687
683
675
678
685
686
672
672
679
677
662
666
669
670
654
653
665
668
652
647
660
662
642
642
655
649
637
634
647
647
632
636
638
633
630
626
630
629
622
615
629
631
619
615
620
619
617
612
612
616
616
611
610
620
614
600
605

Yes, that's normal for a floating input.

Correct me if I am wrong, but does the below code add one to rps once & only once, when analogRead is greater than 1000 and then wait till analogRead is less than 1000 to initiate chk();?

void reed(){ 
  while (analogRead(0) >= 1000){}  //Checked ob ein magnet orbei ist. determies if magnet is present
  rps = rps + 1;
  
  while(analogRead(0) < 1000){}
  chk();  
  }

because it seems like it is adding 1 to rps multiple times and I have no idea why. If the code is not doing what I stated above, what would I have to do to get it to do that?

I think your input is floating. You need something like this !!!

I can't seem to add the jpg file. Send your email and I'll send it directly to you.

I added an FTP site to my web.

This is what you need to start with. The micro switch should switch to ground so if there is a problem with the wring, you dont blow the 5 volt supply.

Cheers Wallace

Sorry I dont know what you mean by adding !!!! where do I have to put them?