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;
     
    }
   
  }
     
}


Computing Stream Lab Book Assessment

Week 4 - Lab 2

21
void setup() {
 
  // Initialize the serial communications
  Serial.begin(9600);
 
}

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

  // Say hello to the world
  Serial.println("Hello World");

  // Wait one second and do it again
  delay(1000);
}
 


22a
void setup() {

  // Initialize the serial communication
  Serial.begin(9600);

}

// -------------------------------------------------------
// The loop() method runs over and over again
// -------------------------------------------------------
void loop() {
 
  byte incomingByte;
 
  // See if there's incoming serial data
  if (Serial.available() > 0) {

  // Read the bytes in the serial buffer
  incomingByte = Serial.read();
  Serial.println(incomingByte, BYTE);
 
  }

}
 




22b

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  char myString[20];
  int index=0;
  while(Serial.available()>0)
  {
  myString[index]=Serial.read();
  index++;
  delay(10);
  }
 
  myString[index]=0;
  if (myString[0] !=0)
  {
  for (int i=0;myString[i]!=0;i++)
  {
  Serial.print(myString[i]);
  }
  Serial.println(".");
  }
}




231
int ledPin=13;

void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int numFlashes=0;
  byte incomingByte;
 
  if(Serial.available()>0)
  {
  incomingByte=Serial.read(); //Reade a single byte
  if ((incomingByte>='0')&&(incomingByte<='9')){
  numFlashes=int(incomingByte-'0');
  }
  }
  for(int i=0;i<numFlashes;i++)
  {
  digitalWrite(ledPin,HIGH); //Make the LED Flash
  delay(100);
  digitalWrite(ledPin,LOW);
  delay(100);
  }
}



 23b
int ledPin=13;

void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int numFlashes=0;
  byte incomingByte;
  int digit;
 
  while(Serial.available()>0) //if-while
  {
  incomingByte=Serial.read(); //Reade a single byte
  digit=int(incomingByte-'0'); //this line is added
  if ((incomingByte>='0')&&(incomingByte<='9')){
  numFlashes=10*numFlashes+digit; //???Is numFlashes defined?
  }
  }
  for(int i=0;i<numFlashes;i++)
  {
  digitalWrite(13,HIGH); //Make the LED Flash
  delay(1000);
  digitalWrite(13,LOW);
  delay(1000);
  }
}



23c

int ledPin=13;

void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int numFlashes=0;
  byte incomingByte;
  int digit; //add
  int count; //add
  int numBytes; //add
 
 numBytes=Serial.available();
 if(numBytes>0)
 {
  delay(100);
  numBytes=Serial.available();
  for(int i=0;i<numBytes;i++) //in serial buffer
  {
  incomingByte=Serial.read();
  if ((incomingByte>='0')&&(incomingByte<='9'))
  {
  digit=int(incomingByte-'0');
  numFlashes=10*numFlashes+digit; //???Is numFlashes defined?
  }
  }
  count=0;
  for(int i=0;i<numFlashes;i++)
  {
  digitalWrite(ledPin,HIGH); //Make the LED Flash
  delay(100);
  digitalWrite(ledPin,LOW);
  delay(100);
  count++;
  }
  Serial.print("Pin13 flashed ");
  Serial.print(count,DEC);
  Serial.println(" times");
 }
}


31
int sensorPin = 0;
int data[100];
float smoothValue = 0;
float centredData = 0;

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

 // Set the reference voltage for analog input to 5 volts
analogReference(DEFAULT);

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

}

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

  Serial.println("Collection data from AlanogPin0");

// Read 100 values into a data array
for (int i = 0; i < 100; i++) {
 // Read the value from the sensor, into the data array
data[i] = analogRead(sensorPin);

 // Wait for 1msec
delay(1);


 }
 // Print the data array
for (int i = 0; i < 100; i++) {
Serial.print(data[i], DEC);
// Update the smoothed value
smoothValue = 0.9 * smoothValue + 0.1 * (float)data[i];

// Calculate the centred data
centredData = data[i] + (512 - (int)smoothValue);
for (int j = 0; j < centredData/20; j++){
Serial.print(" ");
}
Serial.println("#");

// Print the data to the Serial Monitor ?


}

// Pause for the user to read the chart
delay(10000);

}




31 better
int sensorPin = 0;
int data[100];
float smoothValue;
int centredData;

// -------------------------------------------------------
// The setup() method runs once, when the sketch starts
// -------------------------------------------------------
void setup(){
 
  // Set the reference voltage for analog input to 5 volts
  analogReference(DEFAULT);

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

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

  Serial.println("Collection data from AlanogPin0");

  // Read 100 values into a data array
  for (int i = 0; i < 100; i++) {
 
  // Read the value from the sensor
  data[i] = analogRead(sensorPin);
 
  // Wait for 1msec
  delay(1);
 
  }

  // Initialise the exponential smoothing
  smoothValue = 512;
 
  // Print the data arra
  for (int i = 0; i < 100; i++) {
 
  // Update the smoothed value
  smoothValue = 0.9 * smoothValue + 0.1 * (float)data[i];
 
  // Calculate the centred data
  centredData = data[i] + (512 - (int)smoothValue);
 
  // Print the data to the Serial Monitor
  Serial.print(data[i], DEC);
 
  // print a graph of the data
  for (int j = 0; j < centredData/20; j++) Serial.print(" ");
  Serial.println("#");
 
  }
 
  // Pause for the user to read the chart
  delay(10000);
 
}



Monday, 30 May 2011

Arm & Lever - ENGG1000 SNAFU

It is the partial design of the whole system including 'arm' and 'lever'.