Advertisement
Guest User

SV650 Arduino Based Gear Indicator

a guest
May 3rd, 2013
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1.  
  2.   const int bottomLeft = 3;
  3.   const int bottom = 6;
  4.   const int bottomRight = 2;
  5.  
  6.   const int topRight = 7;  
  7.   const int top = 1;  
  8.   const int topLeft = 4;
  9.  
  10.   const int middle = 5;
  11.  
  12.  
  13. void setup() {
  14.  
  15.   pinMode(bottomLeft, OUTPUT);
  16.   pinMode(bottom, OUTPUT);
  17.   pinMode(bottomRight, OUTPUT);
  18.  
  19.   pinMode(topRight, OUTPUT);
  20.   pinMode(top, OUTPUT);  
  21.   pinMode(topLeft, OUTPUT);
  22.  
  23.   pinMode(middle, OUTPUT);
  24.  
  25.   pinMode(A0, INPUT);
  26.  
  27.   //debug
  28.   //Serial.begin(9600);
  29. }
  30.  
  31.  
  32. int incomingByte = 0;
  33. int prevByte = 2;
  34. int currentByte = 0;
  35. int testing = 1;
  36.  
  37. void loop() {
  38.   if (testing == 1){
  39.     test();
  40.     //digitalWrite(6,HIGH); // test individual segments    
  41.   }else{
  42.   int raw = 0;
  43.   float val = 0;
  44.   float avgVal = 0;
  45.  
  46.   for (int i=0; i <= 99; i++){
  47.       raw = analogRead(A0); //  read 0-5 volts from the GPS
  48.       val = fmap(raw, 0, 1023, 0.0, 5.0); // volts are 10 bit (0-1023) so map it back to a proper volt number
  49.       avgVal = avgVal+val;
  50.   }
  51.   avgVal = avgVal/100;
  52.   //Serial.println(avgVal); //voltage
  53.  
  54.   //1st: 1.36V
  55.   //2nd: 1.77V
  56.   //3rd: 2.49V
  57.   //4th: 3.23V
  58.   //5th: 4.1V
  59.   //6th: 4.55V
  60.   //N: 5V
  61.  
  62. if (avgVal <= 1.70){
  63.   reset();
  64.   first();
  65. }
  66.  
  67. if (avgVal >= 1.71 && avgVal <= 2.50){
  68.   second();
  69. }
  70.  
  71. if (avgVal >= 2.51 && avgVal <= 3.1){
  72.   third();
  73. }
  74.  
  75. if (avgVal >= 3.11 && avgVal <= 4.00){
  76.   fourth();
  77. }
  78.  
  79. if (avgVal >= 4.01 && avgVal <= 4.40){
  80.   fifth();
  81. }
  82.  
  83. if (avgVal >= 4.41 && avgVal <= 4.80){
  84.   sixth();
  85. }
  86.  
  87. if (avgVal >= 4.81){
  88.   neutral();
  89. }
  90.   delay(1);
  91.     }
  92. }
  93.  
  94. void reset(){  
  95.   digitalWrite(bottomRight,HIGH);  
  96.   digitalWrite(bottom,HIGH);
  97.   digitalWrite(bottomLeft,HIGH);
  98.    
  99.   digitalWrite(topRight,HIGH);
  100.   digitalWrite(top,HIGH);  
  101.   digitalWrite(topLeft,HIGH);
  102.  
  103.   digitalWrite(middle,HIGH);
  104. }
  105.  
  106.  
  107. void first(){
  108.   reset();
  109.   digitalWrite(topRight,LOW);
  110.   digitalWrite(bottomRight,LOW);
  111. }
  112.  
  113. void second(){
  114.   reset();
  115.   digitalWrite(top,LOW);
  116.   digitalWrite(topRight,LOW);
  117.   digitalWrite(middle,LOW);
  118.   digitalWrite(bottomLeft,LOW);
  119.   digitalWrite(bottom,LOW);
  120. }
  121. void third(){
  122.   reset();
  123.   digitalWrite(top,LOW);
  124.   digitalWrite(topRight,LOW);
  125.   digitalWrite(middle,LOW);
  126.   digitalWrite(bottomRight,LOW);
  127.   digitalWrite(bottom,LOW);
  128. }
  129. void fourth(){
  130.   reset();
  131.   digitalWrite(topLeft,LOW);
  132.   digitalWrite(middle,LOW);
  133.   digitalWrite(topRight ,LOW);
  134.   digitalWrite(bottomRight,LOW);
  135. }
  136. void fifth(){
  137.   reset();
  138.   digitalWrite(top,LOW);
  139.   digitalWrite(topLeft,LOW);  
  140.   digitalWrite(middle,LOW);
  141.   digitalWrite(bottomRight,LOW);
  142.   digitalWrite(bottom,LOW);
  143. }
  144. void sixth(){
  145.   reset();
  146.   digitalWrite(top,LOW);
  147.   digitalWrite(topLeft,LOW);
  148.   digitalWrite(bottomLeft,LOW);
  149.   digitalWrite(bottom,LOW);
  150.   digitalWrite(bottomRight,LOW);
  151.   digitalWrite(middle,LOW);
  152. }
  153. //animated neutral "spins"
  154. void neutral(){  
  155.   digitalWrite(bottomLeft,LOW);
  156.   delay(100);
  157.   reset();
  158.   digitalWrite(bottom,LOW);
  159.   delay(100);
  160.   reset();
  161.   digitalWrite(bottomRight,LOW);
  162.   delay(100);
  163.   reset();
  164.   digitalWrite(topRight,LOW);
  165.   delay(100);
  166.   reset();
  167.   digitalWrite(top,LOW);
  168.   delay(100);
  169.   reset();
  170.   digitalWrite(topLeft,LOW);
  171.   delay(100);
  172.   reset();  
  173. }
  174.  
  175. void test(){
  176.  
  177.  
  178.   first();
  179.   delay(1000);
  180.  
  181.   second();
  182.   delay(1000);
  183.  
  184.   third();
  185.   delay(1000);
  186.  
  187.   fourth();
  188.   delay(1000);
  189.  
  190.   fifth();
  191.   delay(1000);
  192.  
  193.   sixth();
  194.   delay(1000);
  195. }
  196.  
  197. //Mapping function, used to convert 0-1023 to 0-5 volts
  198. float fmap(float x, float in_min, float in_max, float out_min, float out_max)
  199. {
  200.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement