SHARE
TWEET

Untitled

a guest Sep 6th, 2013 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int n1=0;
  2. int n2=0;
  3. int n3=0;
  4. int s1=0;
  5. int s2=0;
  6. int s3=0;
  7. int cont=0;
  8. int number = 1;
  9. int numberp = 1;
  10. int gdis,gspd;
  11.  
  12.   static long receivedNumber = 0;
  13.   static long receivedSpeed = 0;
  14.  
  15.   static boolean negative = false;
  16.   static int distance = 0;
  17.   const char startOfNumberDelimiter = '<';
  18.   const char endOfNumberDelimiter   = '>';
  19.   const char startOfSpeedDelimiter = 'S';
  20.   const char endOfSpeedDelimiter = 's';
  21.  
  22. void setup() {
  23.   pinMode(2,OUTPUT);
  24.   pinMode(3,OUTPUT);
  25.   pinMode(4,OUTPUT);
  26.   pinMode(5,OUTPUT);
  27.   pinMode(6,OUTPUT);
  28.   pinMode(7,OUTPUT);
  29.   pinMode(8,OUTPUT);
  30.   pinMode(9,OUTPUT);
  31.   pinMode(10,OUTPUT);
  32.   pinMode(11,OUTPUT);
  33.   pinMode(12,OUTPUT);
  34.   pinMode(A0,OUTPUT);
  35.   pinMode(A1,OUTPUT);
  36.   pinMode(A2,OUTPUT);
  37.   pinMode(A5,INPUT);
  38.   number = digitalRead(A5);
  39.   numberp = digitalRead(A5);
  40.   Serial.begin(9600);
  41. }
  42.  
  43. void processNumber (const long n) {
  44.   if (gdis == 1) {
  45.     n1=-1;
  46.     n2=-1;
  47.     n3=-1;
  48.   }
  49.   else {
  50.     n1 = n%10;
  51.     if (n >= 10)
  52.       n2 = ((n-n1)/10)%10;
  53.     else
  54.       n2 = 0;
  55.     if (n>=100)
  56.       n3 = (n-n1-10*n2)/100;
  57.     else
  58.       n3 = 0;
  59.   }
  60. }  // end of processNumber
  61.  
  62. void processSpeed (const long n) {
  63.   if (gspd==0) {
  64.     s1 = n%10;
  65.     s2 = ((n-s1)/10)%10;
  66.     s3 = ((n-s1-10*s2)/100)%10;
  67.     processNumber((n-s1-10*s2-100*s3)/1000);
  68.   }
  69.   else {
  70.     s1=-1;
  71.     s2=-1;
  72.     s3=-1;
  73.     processNumber(n/1000);
  74.   }
  75. }
  76.  
  77. void processInput ()
  78.   {
  79.  
  80.   byte c = Serial.read ();
  81.  
  82.   switch (c)
  83.     {
  84.      
  85.     case endOfNumberDelimiter:  
  86.       if (negative)
  87.         processNumber (- receivedNumber);
  88.       else
  89.         processSpeed (receivedNumber);
  90.       distance=0;
  91.      break;
  92.    
  93.     // fall through to start a new number
  94.     case startOfNumberDelimiter:
  95.       cont = 0;
  96.       gdis=0;
  97.       gspd=0;
  98.       distance = 1;
  99.       receivedNumber = 0;
  100.       negative = false;
  101.       break;
  102.      
  103.     case '0' ... '9':
  104.         cont++;
  105.         receivedNumber *= 10;
  106.         receivedNumber += c - '0';
  107.       break;
  108.      
  109.     case '-':
  110.       if (cont < 3) {
  111.         cont++;
  112.         gdis=1;
  113.       }
  114.       else
  115.         gspd=1;
  116.       receivedNumber *=10;
  117.       break;
  118.      
  119.     } // end of switch  
  120.   }  // end of processInput
  121.  
  122. void loop() {
  123.   if (Serial.available ())
  124.     processInput ();
  125.    
  126.   if (negative == false) {
  127.     digitalWrite(10,LOW);
  128.     digitalWrite(11,LOW);
  129.     digitalWrite(12,HIGH);
  130.     digitalWrite(14,LOW);
  131.     digitalWrite(15,LOW);
  132.     digitalWrite(16,LOW);
  133.     writeNumber(n1,0);
  134.     delay(2);
  135.    
  136.     digitalWrite(10,LOW);
  137.     digitalWrite(11,HIGH);
  138.     digitalWrite(12,LOW);
  139.     digitalWrite(14,LOW);
  140.     digitalWrite(15,LOW);
  141.     digitalWrite(16,LOW);
  142.     writeNumber(n2,1);
  143.     delay(2);
  144.    
  145.     digitalWrite(10,HIGH);
  146.     digitalWrite(11,LOW);
  147.     digitalWrite(12,LOW);
  148.     digitalWrite(14,LOW);
  149.     digitalWrite(15,LOW);
  150.     digitalWrite(16,LOW);
  151.     writeNumber(n3,0);
  152.     delay(2);
  153.    
  154.     digitalWrite(10,LOW);
  155.     digitalWrite(11,LOW);
  156.     digitalWrite(12,LOW);
  157.     digitalWrite(14,LOW);
  158.     digitalWrite(15,LOW);
  159.     digitalWrite(16,HIGH);
  160.     writeNumber(s1,0);
  161.     delay(2);
  162.    
  163.     digitalWrite(10,LOW);
  164.     digitalWrite(11,LOW);
  165.     digitalWrite(12,LOW);
  166.     digitalWrite(14,LOW);
  167.     digitalWrite(15,HIGH);
  168.     digitalWrite(16,LOW);
  169.     writeNumber(s2,0);
  170.     delay(2);
  171.    
  172.     digitalWrite(10,LOW);
  173.     digitalWrite(11,LOW);
  174.     digitalWrite(12,LOW);
  175.     digitalWrite(14,HIGH);
  176.     digitalWrite(15,LOW);
  177.     digitalWrite(16,LOW);
  178.     writeNumber(s3,0);
  179.     delay(2);
  180.  
  181.   }
  182.   else {
  183.     digitalWrite(10,HIGH);
  184.     digitalWrite(11,HIGH);
  185.     digitalWrite(12,HIGH);
  186.     digitalWrite(14,HIGH);
  187.     digitalWrite(15,HIGH);
  188.     digitalWrite(16,HIGH);
  189.     digitalWrite(2,LOW);
  190.     digitalWrite(3,HIGH);
  191.     digitalWrite(4,HIGH);
  192.     digitalWrite(5,HIGH);
  193.     digitalWrite(6,HIGH);
  194.     digitalWrite(7,HIGH);
  195.     digitalWrite(8,HIGH);
  196.     digitalWrite(9,HIGH);
  197.     delay(2);
  198.   }
  199.  
  200.   number = digitalRead(A5);
  201.   if (number != numberp) {
  202.     numberp = number;
  203.     Serial.println(number);
  204.   }
  205.     //delay(5);
  206. }
  207.  
  208. void writeNumber(int i, int p) {
  209.   if (p==1)
  210.     digitalWrite(9,LOW);
  211.   else
  212.     digitalWrite(9,HIGH);
  213.   switch (i) {
  214.     case -1:
  215.       digitalWrite(7,LOW);
  216.       digitalWrite(6,HIGH);
  217.       digitalWrite(3,HIGH);
  218.       digitalWrite(2,HIGH);
  219.       digitalWrite(4,HIGH);
  220.       digitalWrite(5,HIGH);
  221.       digitalWrite(8,HIGH);
  222.       digitalWrite(9,HIGH);
  223.     break;
  224.     case 0:
  225.       digitalWrite(7,HIGH);
  226.       digitalWrite(6,LOW);
  227.       digitalWrite(3,LOW);
  228.       digitalWrite(2,LOW);
  229.       digitalWrite(4,LOW);
  230.       digitalWrite(5,LOW);
  231.       digitalWrite(8,LOW);
  232.     break;
  233.     case 1:
  234.       digitalWrite(7,HIGH);  
  235.       digitalWrite(6,HIGH);
  236.       digitalWrite(3,HIGH);
  237.       digitalWrite(2,LOW);
  238.       digitalWrite(4,HIGH);
  239.       digitalWrite(5,HIGH);
  240.       digitalWrite(8,LOW);
  241.     break;
  242.     case 2:
  243.       digitalWrite(7,LOW);
  244.       digitalWrite(6,HIGH);
  245.       digitalWrite(3,LOW);
  246.       digitalWrite(2,LOW);
  247.       digitalWrite(4,LOW);
  248.       digitalWrite(5,LOW);
  249.       digitalWrite(8,HIGH);
  250.     break;
  251.     case 3:
  252.       digitalWrite(7,LOW);
  253.       digitalWrite(6,HIGH);
  254.       digitalWrite(3,LOW);
  255.       digitalWrite(2,LOW);
  256.       digitalWrite(4,HIGH);
  257.       digitalWrite(5,LOW);
  258.       digitalWrite(8,LOW);
  259.     break;
  260.     case 4:
  261.       digitalWrite(7,LOW);
  262.       digitalWrite(6,LOW);
  263.       digitalWrite(3,HIGH);
  264.       digitalWrite(2,LOW);
  265.       digitalWrite(4,HIGH);
  266.       digitalWrite(5,HIGH);
  267.       digitalWrite(8,LOW);
  268.     break;
  269.     case 5:
  270.       digitalWrite(7,LOW);
  271.       digitalWrite(6,LOW);
  272.       digitalWrite(3,LOW);
  273.       digitalWrite(2,HIGH);
  274.       digitalWrite(4,HIGH);
  275.       digitalWrite(5,LOW);
  276.       digitalWrite(8,LOW);
  277.     break;
  278.     case 6:
  279.       digitalWrite(7,LOW);
  280.       digitalWrite(6,LOW);
  281.       digitalWrite(3,LOW);
  282.       digitalWrite(2,HIGH);
  283.       digitalWrite(4,LOW);
  284.       digitalWrite(5,LOW);
  285.       digitalWrite(8,LOW);
  286.     break;
  287.     case 7:
  288.       digitalWrite(7,HIGH);
  289.       digitalWrite(6,HIGH);
  290.       digitalWrite(3,LOW);
  291.       digitalWrite(2,LOW);
  292.       digitalWrite(4,HIGH);
  293.       digitalWrite(5,HIGH);
  294.       digitalWrite(8,LOW);
  295.     break;
  296.     case 8:
  297.       digitalWrite(7,LOW);
  298.       digitalWrite(6,LOW);
  299.       digitalWrite(3,LOW);
  300.       digitalWrite(2,LOW);
  301.       digitalWrite(4,LOW);
  302.       digitalWrite(5,LOW);
  303.       digitalWrite(8,LOW);
  304.     break;
  305.     case 9:
  306.       digitalWrite(7,LOW);
  307.       digitalWrite(6,LOW);
  308.       digitalWrite(3,LOW);
  309.       digitalWrite(2,LOW);
  310.       digitalWrite(4,HIGH);
  311.       digitalWrite(5,HIGH);
  312.       digitalWrite(8,LOW);
  313.     break;
  314.     default:
  315.     break;
  316.   }
  317. }
  318.  
  319. //     4
  320. //    ---
  321. // 3 |   | 5
  322. //    -2-
  323. // 6 |   | 8
  324. //    ---
  325. //     7
  326.  
  327.  
  328.  
  329. //  Orden derecha izquierda
  330. //  Negro (9), negro (8), verde, naranja, rojo, blanco, amarillo, azul
  331. //  Orden por resistencia (izquierda -> derecha desde arriba)
  332. //  5, 4, 6, 7, 3, 2, 8, 9
  333. //  Distancia controles: 10,11,12
  334. //  Velocidad controles: A0,A1,A2
  335. //  Interruptor: A5
  336. //  2, 3, 4, 5, 6, 7, 8, 9
RAW Paste Data
Want to get better at C?
Learn to code C in 2017
Top