Programm Throttle2

/*
  Throttle2 - on Arduino Hardware
  reads Speed reading (analog) and several switches for direction
  and Functions F0..F3 via SerialPort (USB)

  like for example T1S99VF1000 
  T1 = Throttle1
  S99 = Speed 99
  F1000 = F0set, F1,F2,F3 unset

Copyright (C) 2009 Michael Blank

This program is free software; you can redistribute it and/or modify it under the terms of the G
NU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, see .
 */
 int ledPin = 13;  
 int dirPin = 12;
 int FPin[] = { 8,9,10,11};
 int maxF =3;
 int last_analogValue = 128;
 int last_dir = 2;
 int func[] = {0,0,0,0};
 int last_func[] = {0,0,0,0};
 int last_f[] = { 0,0,0,0 };
 
 unsigned long last_sent=0;
 
 boolean changed = false; // send message only when something changed
 
 void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(dirPin, INPUT);
    digitalWrite(dirPin, 1);  //enable pull-up resistor
    for (int i=0 ; i<=maxF; i++){
       pinMode(FPin[i], INPUT);
       digitalWrite(FPin[i], 1);  //enable pull-up resistor
    }  
    Serial.begin(9600); 
 }
 
 void loop() {
   changed = false; // send message only when something changed
   
   // read the analog input into a variable:
   int analogValue;
   // limit range to 0..127
   analogValue = (127L * analogRead(0))/1023;
   if (analogValue != last_analogValue) { 
      changed = true;  
      last_analogValue = analogValue;
   }

   int dir = digitalRead(dirPin);
   if (dir != last_dir)  {  
      changed = true;  
      last_dir = dir;
   }

   // check digital in function pins for change
   int f[] = {0,0,0,0};
   for (int i=0 ; i<=maxF; i++){
       f[i] = digitalRead(FPin[i]);
       if ( (f[i] == 1) && ( last_f[i] == 0) ) { 
           // toggle
           if (func[i] == 1)  {
               func[i] =0;
           }  else  {
               func[i] =1;
           }    
       }
       last_f[i] = f[i];
   }
   
   for (int i=0 ; i<=maxF; i++){
       if (func[i] != last_func[i]) { 
           // any function changed
           changed = true; 
           last_func[i] = func[i];
       }
   }
   
   // send ASCII message when a value has changed or 
   // at least every 2 seconds
   if ( (changed) || ( (millis() - last_sent) >2000 ) ) {
      Serial.print("T1"); //throttle 1
      // print the result:
      Serial.print("S");
      Serial.print(analogValue);
      if (dir == 1) {
         Serial.print("V"); // vorwaerts
      } else {
         Serial.print("R"); // rueckwaerts
      }
      Serial.print("F");  // function keys
      for (int i=0 ; i<=maxF; i++){
          if (func[i] == 1) {
            Serial.print("0");
          } else  {
          Serial.print("1");
          }
      }
      Serial.println();
      last_sent = millis();
   }
   // wait 20 milliseconds
   delay(20);
 }