For Loops with Arduino

For Loops with Arduino

by Michael Backus -
Number of replies: 3

I want to write a series of questions that force students to really understand looping. I expect these would be small programs that would output a list of expected numbers. For example:

  1. Write a program that uses a loop to print the numbers 0-5 to the serial monitor.
  2. Write a program that uses a loop to print the numbers 0-10 to the serial monitor.
  3. Write a program that uses a loop to print the numbers 3-12 to the serial monitor.
  4. Write a program that uses a loop to print the following to the serial monitor: 0,2,4,6,8,10.
  5. Write a program that uses a loop to print the following to the serial monitor: 0,5,10,15,20,25,30.
  6. Write a program that uses a loop to count down from 5 to 0 and prints those numbers to the serial monitor.

I think the best way to ask these types of questions is to use the CodeRunner question type, but I'm not exactly sure how.

To make matters more complicated, I would like to ask the question using C/C++, but modified so that it appears to be ArduinoC to the students (unless of course there's a way to setup my jobe server to handle ArduinoC). To make matters even more complicated, I would also like to make this an Arduino via Python3 question type so that I could use pylint to check for things like white space and proper positioning of curly braces and whatnot.

The correct solution would look like:

void setup()
{
  Serial.begin(9600);
  
  for(int x = 0; x < 5; x++)
    Serial.println(x);
}

void loop()
{
}
Ideally the students would need to input everything since part of their confusion arises from not knowing whether they should add their code to the setup or the loop.

Also, is there a way to make the inputs into the for loop variables so that the answers to the 2nd, 3rd, 4th, etc. questions vary? I'd like the quiz questions to vary from student to student to make cheating a little harder.
In reply to Michael Backus

Re: For Loops with Arduino

by Richard Lobb -

I think you'll find quite a few of your questions are answered in this forum posting:

https://coderunner.org.nz/mod/forum/discuss.php?d=155

pylint won't help you check the syntax of C, although if you use a "C via Python" question type (modified to use C++) you can at least write Python code to do a lot of checks; I'm guessing that's what you meant, anyway.

It sounds like you're also wishing to use randomisation to present different questions to different students. This is possible - randomisation is documented here -  but I'd suggest you hold off until you have a bit more experience with CodeRunner. 

In reply to Richard Lobb

Re: For Loops with Arduino

by Michael Backus -

I was actually the one who emailed you the questions in that forum posting :)

At any rate, I'll proceed as you suggest with a C/C++ via Python question type. As for the checks, are there any examples that show how to run checks for indentation and whatnot?

Also, one question I forgot to ask was how to deal with students circumventing the task by simply typing

Serial.print(0);
Serial.print(1);
Serial.print(2);
Serial.print(3);
Serial.print(4);
Serial.print(5);
What's the best way to prevent that? My first thought is to use Python to search for "Serial.print" and reject anything that returns more than one instance. Is there a better approach? Thanks for all the help.
In reply to Michael Backus

Re: For Loops with Arduino

by Richard Lobb -

Hah! I should have noticed the name was the same. Sorry about that :)

Checking for the right layout is difficult. Currently our approach in the C programming course is to require students to submit code that is laid out in according to the 1TBS style. We give them the astyle program and tell them to run their code through that before submitting. Then we have a style-checker phase within CodeRunner that verifies that their code is unaltered by again being run through astyle. If there are any differences, their submission is thrown out. We have both a Precheck and a Check button and the style checking is all done without penalty via the Precheck (and again by Check but they shouldn't be clicking that button if they have failed the Precheck). 

The best way to prevent them circumventing the task by providing 6 different calls to Serial.print is probably to have multiple tests, with each test defining a global variable or perhaps a #define of the number of print calls required. Having at least one hidden test is always a good idea to prevent students constructing an artificial solution that works only for a known set of tests.

Alternatively, before running their code you can check that it satisfies various conditions such as (in this case) only a single Serial.print call.