Thursday, 25 August 2011

Lab 4_Week 6

41

// -------------------------------------------------------
// Global Variables
// -------------------------------------------------------
int interruptPin = 2;
int interruptNo = 0;
long interruptTime[100];
int interruptState[100];
int dataLength = 0;

// -------------------------------------------------------
// Interrupt Event Handler
// -------------------------------------------------------
void interruptPinChange(){
 
  // Log the time of the interrupt
  interruptTime[dataLength] = micros();
 
  // Record the pin state
  interruptState[dataLength] = (digitalRead(interruptPin) == HIGH);
 
  // Increment the length of the data log
  dataLength++;
 
}

// -------------------------------------------------------
// The setup() method runs once, when the sketch starts
// -------------------------------------------------------
void setup(){ 
 
  // Set up the interupt
  pinMode(interruptPin, INPUT);
  attachInterrupt(interruptNo, interruptPinChange, CHANGE);

  // initialize the serial communications
  Serial.begin(9600);
  Serial.println("Initialised");
 
}

// -------------------------------------------------------
// The loop() method runs over and over again
// -------------------------------------------------------
void loop(){
 
  // Use the USB Serial Comms as a substitute for a genuine
  // signal coming into InterruptPin0
  // Connect Digital IO Pins 0 & 2
 
  // Has there been a transmission
  if (Serial.available() > 0) {
   
    // Wait for all the data to arrive
    delay(100);
   
    // Flush the buffer
    Serial.flush();
   
    // Is there any data to view
    if (dataLength > 1) {
     
      // Print the data log
      Serial.println("Log begins");
      for(int i = 1; i < dataLength; i++) {
       
        // Print the data log
        Serial.print(interruptTime[i] - interruptTime[i-1], DEC);
        Serial.print(" - ");
        Serial.println(interruptState[i-1], DEC);

      }
      Serial.println("Log ends");
     
      // Reset the data log
      dataLength = 0;
     
    }
   
  }
     
}




42

// =======================================================
// ENGG1000 - Computing Techincal Stream
// Interrupt
// Wtitten by Michael Schofield August 2011
// Implements an Interupt to reads incoming serial comms
// =======================================================

// -------------------------------------------------------
// Global Variables
// -------------------------------------------------------
int interruptPin = 2;
int interruptNo = 0;
long interruptTime;
int interruptState[100];
int dataLength = 0;
int pulseWidth;
long prevInterruptTime;
int pulseValue;
int pulse[100];
int i;


// -------------------------------------------------------
// Interrupt Event Handler
// -------------------------------------------------------
void interruptPinChange(){

  // Log the time of the interrupt
  interruptTime = micros();

  // Calculate the time elapsed since the previous state change of the pin
  pulseWidth = (interruptTime - prevInterruptTime + 52) / 104;
  prevInterruptTime = interruptTime;

  // Has there been a long delay since any communications
  if (abs(pulseWidth) > 10) return;

  // Evaluate the meaning of the pulse
  if (digitalRead(interruptPin) == LOW) {
    pulseValue = (int) pulseWidth;
  } else {
    pulseValue = -(int) pulseWidth;
  }

  // Strore the value of the pulse in a data log
  pulse[dataLength] = pulseValue;
  dataLength++;

}

// -------------------------------------------------------
// The setup() method runs once, when the sketch starts
// -------------------------------------------------------
void setup(){ 

  // Set up the interupt
  pinMode(interruptPin, INPUT);
  attachInterrupt(interruptNo, interruptPinChange, CHANGE);

  // initialize the serial communications
  Serial.begin(9600);
  Serial.println("Initialised");

}

// -------------------------------------------------------
// The loop() method runs over and over again
// -------------------------------------------------------
void loop(){

  // Use the USB Serial Comms as a substitute for a genuine
  // signal coming into InterruptPin0
  // Connect Digital IO Pins 0 & 2

  // Has there been a transmission
  if (Serial.available() > 0) {
   
    // Wait for all the data to arrive
    delay(100);
   
    // Flush the buffer
    Serial.flush();
   
    // Is there any data to view
 // Print the data log
    
      for(int i = 1; i < dataLength; i++) {
       
      // Was it a positive or negative pulse
      if (pulse[i] > 0) {
        for (int j = 0; j < pulse[i]; j++) Serial.print(175, BYTE);
      } else {
        for (int j = 0; j > pulse[i]; j--) Serial.print("_");
      }

      }
  
     
      // Reset the data log
      dataLength = 0;
     
  }
}



43
// =======================================================
// ENGG1000 - Computing Techincal Stream
// Interrupt
// Implements an Interupt to reads incoming serial comms
// =======================================================

// -------------------------------------------------------
// Global Variables
// -------------------------------------------------------
int interruptPin = 2;
int interruptNo = 0;
long interruptTime;
int interruptState[100];
int dataLength = 0;
int i = 0;
int pulseValue = 0;
int pulse[100];
long pulseWidth = 0;
long prevInterruptTime = 0;

// -------------------------------------------------------
// Interrupt Event Handler
// -------------------------------------------------------
void interruptPinChange(){

  // Log the time of the interrupt
  interruptTime = micros();

  // Record the pin state
  interruptState[dataLength] = (digitalRead(interruptPin) == HIGH);
  // Calculate the time elapsed since the previous state change of the pin
  pulseWidth = (interruptTime - prevInterruptTime + 52) / 104;
  prevInterruptTime = interruptTime;

  // Has there been a long delay since any communications
  if (abs(pulseWidth) > 10) return;

  // Evaluate the meaning of the pulse
  if (digitalRead(interruptPin) == LOW) {
    pulseValue = (int) pulseWidth;
  } else {
    pulseValue = -(int) pulseWidth;
  }

  // Strore the value of the pulse in a data log
  pulse[dataLength] = pulseValue;
  dataLength++;

}

// -------------------------------------------------------
// The setup() method runs once, when the sketch starts
// -------------------------------------------------------
void setup(){ 

  // Set up the interupt
  pinMode(interruptPin, INPUT);
  attachInterrupt(interruptNo, interruptPinChange, CHANGE);

  // initialize the serial communications
  Serial.begin(9600);
  Serial.println("Initialised");

}

// -------------------------------------------------------
// The loop() method runs over and over again
// -------------------------------------------------------
void loop(){

  // Use the USB Serial Comms as a substitute for a genuine
  // signal coming into InterruptPin0
  // Connect Digital IO Pins 0 & 2

  // Has there been a transmission
  if (Serial.available() > 0) {
   
    // Wait for all the data to arrive
    delay(100);
   
    // Flush the buffer
    Serial.flush();
   
    // Is there any data to view
    if (dataLength > 1) {
     
      // Print the data log
     
      for(int i = 1; i < dataLength; i++) {
        // Was it a positive or negative pulse
        if ((pulse[i] == 1)) {
          //for (int j = 0; j < pulse[i]; j++)
          Serial.print(".");
        } else if ( (pulse[i] >= 2)) {
          //for (int j = 0; j < pulse[i]; j++)
          Serial.print("-");
        } else if (pulse[i] == -1) {
          //for (int j = 0; j > pulse[i]; j--)
          Serial.print("");
        } else if (pulse[i] == -2) {
          //for (int j = 0; j > pulse[i]; j--)
          Serial.print("|");
        } else if (pulse[i] <= -3) {
          //for (int j = 0; j > pulse[i]; j--)
          Serial.print("#");
        }


      }
     
      // Reset the data log
      dataLength = 0;
     
    }
   
  }
     
}


No comments:

Post a Comment