Thursday, 25 August 2011

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



No comments:

Post a Comment