Homework Code

This will become a list of possible programs to solve the coding homework problems. There area usually other correct ways to solve the assignments but these are simple ones that work.

NOTE that I am not going to show includes and header comments. Those must always be present, but I trust that you know that part by now.

H3 Prob. 12.

Write a computer program that sits reading characters from the serial port and echoing them back in the opposite case. That is, if the user types 'a' then the program should send back 'A', if the user types 'Q', the program should send back 'q', etc.

Solution

void main(void) {
	//
	//	Declare any variables that you use here.
	//
	char theChar;
	//
	//	Do any one-time setup here
	//
	Serial_begin(9600);
	printf("Enter the text to convert\r\n");
	//
	//	Code runs in an endless loop
	//
	for (;;) {
		theChar = getchar();
		if (('A' <= theChar) && ('Z' >= theChar)) {
			//
			// Found an upper case letter. Print as lower case.
			//
			putchar(theChar + 0x20);
		} else if (('a' <= theChar) && ('z' >= theChar)) {
			//
			// Found a lower case letter. Print as upper case.
			//
			putchar(theChar - 0x20);
		} else {
			//
			// Otherwise should print what they typed.
			//
			putchar(theChar);
		}
	} // end for
} // end main

H4 Prob. 12.

Write a program that sits reading +ve integers from the serial port. It should stop when a zero is entered and then it should print the largest and smallest values that were entered (excluding the zero). Obviously it will then go back and ask for more input numbers and start over again.

Thus the interaction should look something like this

Enter numbers (-ve to end)
23
46
103
27
5
0
The largest value was 103 and the smallest was 5.
Enter numbers....

Solution

/*  
* Largest & Smallest  
* Reads positive integers from the serial port until a negative  
* number is entered. It then prints the largest and smallest   
* values that were entered.  
* BCollett 2/19/2016 
*/

int cVal;			 	// The current value 
int minVal = 32767;   // Lowest value found. All ints are <= this 
int maxVal = -1;	   // The highest value found. Must be >= 0 

void setup(void) {
 	Serial.begin(9600);
 	Serial.setTimeout(-1);	// parseInt now waits for EOL
 	Serial.println(Enter numbers (-ve to end));
 }

void loop(void) {
 	if (Serial.available() > 0) {
 		cVal = Serial.parseNum();
 		if (cVal < 0) {
 			//
 			// Get here only when negative. Print results.
 			//
 			Serial.print(The largest value was ");
 			Serial.println(maxVal);
 			Serial.print("The smallest value was );
 			Serial.println(maxVal);
 			//
 			// start over
 			//
 			Serial.println(Enter numbers (-ve to end));
  		} else if (cVal < minVal) {	// update the minimum?
 			minVal = cVal;
 		} else if (cVal > maxVal) { // update the maximum?
 			maxVal = cVal;
 		}
 	}
}

H5 Prob. 12

Write a program that sits reading single characters from the serial port. It should normally do nothing but any time the user enters the characters 'a', 's', and 'k' in that order, with no intervening characters, the program should print "Shan't!".

Physics 245