Saturday, 18 April 2015

Tasks 21-24(complete)

21. Randoms. Write a program to output random numbers between 0 and 100 to the serial terminal.
//Program to output random numebrs between 1-100 to the serial port
// Arron Dick
// 02/06/2015


void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop(){
  int randomNumber=random(1,100+1);
 Serial.println(randomNumber); 
}

22. Write a program to output the throwing of a dice every second and display the number that comes up.

// Program to output a dice number every second
// Arron Dick
// 02/06/2015


void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop(){
  int randomNumber=random(1,6+1);
 Serial.println(randomNumber);
 delay(1000);
}

23. Same as 22 but display as well the number of sixes that you have thrown so far.
// Program to output a dice number every second
// Arron Dick
// 02/06/2015

int sixesSoFar=0;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop(){
  int randomNumber=random(1,6+1);
  if (randomNumber==6){
   sixesSoFar++; 
  }
  Serial.print("Dice throw: ");
 Serial.println(randomNumber);
 
 Serial.print("Sixes so far: ");
 Serial.println(sixesSoFar);
 
 delay(1000);
}

24. Same as 23 but speed it up and stop when you get to 25 sixes.
// Program to output a dice number every second
// Arron Dick
// 02/06/2015

int sixesSoFar=0;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop(){
  if (sixesSoFar<25){
    int randomNumber=random(1,6+1);
    if (randomNumber==6){
     sixesSoFar++; 
    }
    Serial.print("Dice throw: ");
   Serial.println(randomNumber);
 
   Serial.print("Sixes so far: ");
   Serial.println(sixesSoFar);
 
   delay(100);
  }
}

No comments:

Post a Comment