231a-ag progressdec13

From CSclasswiki
Jump to: navigation, search

Progress Report (as of December 13)

Coding

Arduino Sketch #3

/*
 * Hardware Testing
 *
 * Program that tests the Arduino hardware.
 *
 */

char buffer[256];
int len;
char *operation;
char *mode;
char *pin;
char *state;

int ledPin7 = 7;
int ledPin8 = 8;                   // LED connected to digital pin 8 and 12
int ledPin9 = 9;

int ledPin10 = 10;
int ledPin11 = 11;
int ledPin12 = 12;

int buzzerPin = 13;

int keypadDPin3 = 3;
int keypadDPin4 = 4;
int keypadDPin5 = 5;

int keypadAPin0 = 0;
int keypadAPin1 = 1;
int keypadAPin2 = 2;

int padNumber3 = 0;
int padNumber3a = 0;
int padNumber3b = 0;
int padNumber1 = 0;
int padNumber5 = 0;

int padNumber2 = 0;
int padNumber7 = 0;
int padNumber6 = 0;

int analogReadPin = 0;
int digitalReadPin = 0;

// PROTOTYPES
void resetAll();
int process();
int writePin( char mode, int pin, int state);
int readPins( char mode);

// SETUP
void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin7, OUTPUT);         // sets the digital pin as output
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  
  pinMode(keypadDPin3, OUTPUT);
  pinMode(keypadDPin4, OUTPUT);
  pinMode(keypadDPin5, OUTPUT);
  
  pinMode(keypadAPin0, INPUT);
  pinMode(keypadAPin1, INPUT);
  pinMode(keypadAPin2, INPUT);
  
  Serial.begin(9600);
  resetAll();
}

// RESETALL
void resetAll() {
   buffer[0] = '\0';
   len = 0;
   operation = mode = pin = state = 0;
}

void loop() {                        // run over and over again
  goto led;
  int c;
  c = Serial.read();
  
  // nothing received, nothing to do
  if (c==-1) return;
  
  // \n terminator received, process message
  if ( c=='\n') {
     if ( process () ) {
         Serial.println("Error");
     }
       resetAll();
       return;
  }
  
  // something received,but not \n, add to buffer
  buffer[ len++ ] = c;
  buffer[ len ] = '\0';

led:     
  //digitalWrite(buzzerPin, LOW);    // sets buzzer on
  digitalWrite(keypadDPin3, LOW);
  digitalWrite(keypadDPin4, LOW);
  digitalWrite(keypadDPin5, LOW);

  padNumber3 = analogRead(keypadAPin0);
  padNumber1 = analogRead(keypadAPin1);
  padNumber5 = analogRead(keypadAPin2);
 
  if (padNumber3 < 200) {
    digitalWrite(keypadDPin3, HIGH);
    digitalWrite(keypadDPin4, HIGH);
    digitalWrite(keypadDPin5, LOW);  // Is it 1?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    // KEY PRESSED IS 1
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin9, LOW);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, LOW);
    }
    
    digitalWrite(keypadDPin3, HIGH);
    digitalWrite(keypadDPin4, LOW);
    digitalWrite(keypadDPin5, HIGH);  // Is it 4?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    //  KEY PRESSED IS 4
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin8, LOW);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin10, LOW);
    }  
    
    digitalWrite(keypadDPin3, LOW);
    digitalWrite(keypadDPin4, HIGH);
    digitalWrite(keypadDPin5, HIGH);  // Is it 7?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    // KEY PRESSED IS 7
      digitalWrite(ledPin7, LOW);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin10, LOW);
    }
  } else if (padNumber1 < 200) {
     digitalWrite(keypadDPin3, HIGH);
     digitalWrite(keypadDPin4, HIGH);
     digitalWrite(keypadDPin5, LOW);  // Is it 2?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 2
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, LOW);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);
       delay(100);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin11, LOW);
     }
    
     digitalWrite(keypadDPin3, HIGH);
     digitalWrite(keypadDPin4, LOW);
     digitalWrite(keypadDPin5, HIGH);  // Is it 5?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 5
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, LOW);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);
       delay(100);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin11, LOW);
     }
       
     digitalWrite(keypadDPin3, LOW);
     digitalWrite(keypadDPin4, HIGH);
     digitalWrite(keypadDPin5, HIGH);  // Is it 8?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 8
       digitalWrite(ledPin7, LOW);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);  
       delay(100);
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin11, LOW);
     }
  } else if (padNumber5 < 200) {
      digitalWrite(keypadDPin3, HIGH);
      digitalWrite(keypadDPin4, HIGH);
      digitalWrite(keypadDPin5, LOW);  // Is it 3?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 3
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, LOW);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, LOW);
       digitalWrite(ledPin12, HIGH);  
       delay(100);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin12, LOW);
      }
      
      digitalWrite(keypadDPin3, HIGH);
      digitalWrite(keypadDPin4, LOW);
      digitalWrite(keypadDPin5, HIGH);  // Is it 6?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 6
        digitalWrite(ledPin7, HIGH);
        digitalWrite(ledPin8, LOW);
        digitalWrite(ledPin9, HIGH);  
        digitalWrite(ledPin10, LOW);
        digitalWrite(ledPin11, LOW);
        digitalWrite(ledPin12, HIGH);
        delay(100);
        digitalWrite(ledPin8, HIGH);
        digitalWrite(ledPin12, LOW);
      }
      
      digitalWrite(keypadDPin3, LOW);
      digitalWrite(keypadDPin4, HIGH);
      digitalWrite(keypadDPin5, HIGH);  // Is it 9?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 9
        digitalWrite(ledPin7, LOW);
        digitalWrite(ledPin8, HIGH);
        digitalWrite(ledPin9, HIGH);
        digitalWrite(ledPin10, LOW);
        digitalWrite(ledPin11, LOW);
        digitalWrite(ledPin12, HIGH);
        delay(100);
        digitalWrite(ledPin7, HIGH);
        digitalWrite(ledPin12, LOW);
      }
  }
}
  
// MYSTRTOK: equivalent to C strtok function that doesn't seem
//           supported by arduino.  Given a pointer to a string
//           p, returns a pointer q past p that points to a char
//           that is not a space.  If cannot find one, return
//           NULL.

char* mystrtok( char* p, int skipNonBlank ) {
    char* q=p;
    if ( skipNonBlank ) {
        for ( ; *q != ' ' && *q != '\0'; q++ );
        if ( *q=='\0' ) return NULL;
    }
    for ( ; *q != '\0' && *q==' '; q++ );
    if ( *q == '\0' ) return NULL;
    if ( *q == ' ' ) return NULL;
    return q;
}

// PROCESS: if we're here it's because we have received a 
// message from the UBUNTU PC.
int process() {
    char *p;
    int len = strlen( buffer );
    int intPin;
    int intState;

    //Serial.print( "buffer = " );
    //Serial.print( buffer );
    
    //--- if message less than 3 chars long, return ---
    if ( len < 3 ) {
        resetAll();
        return 1; //error
    }

    //--- get the operation: 'w' or 'r'.  Do some error checking---
    operation = mystrtok( buffer, 0 );

    if ( ( !operation || (*operation!='r' && *operation!='w') ) 
         || ( ( *operation=='r' && len < 3 ) ||( *operation=='w' && len < 7 ) ) ) {
        resetAll();
        return 1; // error
    }

    //--- get the mode: 'd' or 'a' and do some error checking ---
    mode = mystrtok( operation + 1, 0 );
    if (  !mode || ( *mode!='a' && *mode!='d' ) ) {
        resetAll();
        return 1; // success
    }

    //--- if operation is 'r' read the pins ---
    if ( *operation == 'r' ) {
        readPins( *mode );
        resetAll();
        return 0; // success
    }

    //--- if not, operation is 'w', then get pin # and state ---
    pin = mystrtok( mode + 1, 0 );
    if ( !pin ) {
        resetAll();
        return 1; // error
    }
    intPin = atoi( pin );
    if ( intPin < 2 || intPin > 13 ) {
        resetAll();
        return 1; // error
    }
    
    //--- get the state of the output pin ---
    state = mystrtok( pin+1, 1 );
    if ( !state ) {
        resetAll();
        return 1; // error 
    }
    intState = atoi( state );
    if ( intState < 0 || intState > 1 ) {
        resetAll();
        return 1; // error
    }
    
    //--- write the state to the pin in question ---
    writePin( *mode, intPin, intState );

    resetAll();
    return 0;
}

// WRITEPIN: depending on mode, writes the state to the pin
int writePin( char mode, int  intPin, int intState ) {
    if ( mode=='d' )
        digitalWrite( intPin, intState );
    if ( mode=='a' )
        analogWrite( intPin, intState );
    //delay( 50 ); // 0.05 sec
    //Serial.println( "OK" );
    return 0;
}

// READPINS: read the status of all the pins and returns it as
int readPins( char mode ) {
  int i;
  if ( mode=='d' ) {
        digitalReadPin = digitalRead(5);
        if (digitalReadPin==0) {
          analogReadPin = analogRead(0);
          if (analogReadPin<=200) {
            Serial.print("1");
          } 
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(1);
          if (analogReadPin<=200) {
            Serial.print("2");
          }
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(5);
          if (analogReadPin<=200) {
            Serial.print("3");
          }
        }
        digitalReadPin = digitalRead(4);
        if (digitalReadPin==0) {
          analogReadPin = analogRead(0);
          if (analogReadPin<=200) {
            Serial.print("4");
          } 
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(1);
          if (analogReadPin<=200) {
            Serial.print("5");
          }
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(5);
          if (analogReadPin<=200) {
            Serial.print("6");
          }
        }
        digitalReadPin = digitalRead(3);
        if (digitalReadPin==0) {
          analogReadPin = analogRead(0);
          if (analogReadPin<=200) {
            Serial.print("7");
          } 
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(1);
          if (analogReadPin<=200) {
            Serial.print("8");
          }
        } else if (digitalReadPin==0) {
          analogReadPin = analogRead(5);
          if (analogReadPin<=200) {
            Serial.print("9");
          }
        }
  }    // end of if ( mode=='d' )
  
  if ( mode=='a' ) {
        analogReadPin = analogRead(0);
        if (analogReadPin<=200) {
          digitalReadPin = digitalRead(5);
          if (digitalReadPin==0) {
            Serial.print("1");
          } 
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(4);
          if (digitalReadPin==0) {
            Serial.print("4");
          }
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(3);
          if (digitalReadPin==0) {
            Serial.print("7");
          }
        }
        analogReadPin = analogRead(1);
        if (analogReadPin<=200) {
          digitalReadPin = digitalRead(5);
          if (digitalReadPin==0) {
            Serial.print("2");
          } 
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(4);
          if (digitalReadPin==0) {
            Serial.print("5");
          }
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(3);
          if (digitalReadPin==0) {
            Serial.print("8");
          }
        }
        analogReadPin = analogRead(2);
        if (analogReadPin<=200) {
          digitalReadPin = digitalRead(5);
          if (digitalReadPin==0) {
            Serial.print("3");
          } 
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(4);
          if (digitalReadPin==0) {
            Serial.print("6");
          }
        } else if (analogReadPin<=200) {
          digitalReadPin = digitalRead(3);
          if (digitalReadPin==0) {
            Serial.print("9");
          }
        }
  }
  Serial.println();
  return 0;
}

This code should get rid of the error it was generating before!