Help! Simple computational program??

Hey all--

So I am trying to write a very basic computational program in Arduino, but I am brand new to programming and have no idea what I'm doing. I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.

This is what the program should spit out:

1 2 3 4 5 6 7 6 5 4 3 2 1

....I have no idea where to even start. I tried to make some basic logic statements to just get numbers 1 through 7 printed, but I know there must be an easier way, and I'm not sure how I would print the result in a row. This is what I have right now, but the right numbers aren't even being printed :/....can someone please give me a nudge in the right direction? :fearful:

void setup() {

Serial.begin(9600);

int n = 7;

int x = n-6;
int y = x++;
int z = y++;
int a = z++;
int b = a++;
int c = b++;

Serial.println(x);
Serial.println(y);
Serial.println(z);
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(n);

}

void loop(){

}

Please use code tags.

Read this before posting a programming question

milosaurus:
This is what I have right now, but the right numbers aren't even being printed :confused:

OK, that is the first problem you must solve, as otherwise all the other stuff is irrelevant.

Did you mean NOTHING is being printed, then you should run a "hello world" style program:

void setup() {
Serial.begin(9600);
Serial.println("Hello World!");
}
void loop() { }

If this does not show up Hello World in Your Serial monitor window then there first is some basic setup that needs to be worked out.

In your program you use Serial.println() - if you want things to stay on the same line use Serial.print(). You need to space things out a little so include a Serial.print(' '); between each so you get a space. (Yes, you have to every thing yourselv in programming, specify every little detail)

Serial.print(x);Serial.print(' ');
Serial.println(y);Serial.print(' ');

Lastly I think your approach is novel. A new way to solve your challenge. Not efficient or elegant, but you found what should be a workable solution.

To get you steered in the right direction consider reducing the number of variables like

int n=1 ;
Serial.print(n);Serial.print(' ');
n++ ;  // or the equivalent n += 1 ;   or the equivalent n=n+1 ;
Serial.print(n);Serial.print(' ');
n += 1 ;   // does the same thing as above
Serial.print(n);Serial.print(' ');
n=n+1 ;  // does the same thing as above 
Serial.print(n);Serial.print(' ');
n++ ;  // and should generate the same code

This way you only use one variable.

Next, look at http://arduino.cc/en/Reference/For

milosaurus:
I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.

This is homework, right?

I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.

Two for loops - one incrementing from 1 to n, the other decrementing from n-1 to 1. Printing in a column is done with println(). Printing on a row is done with print().

You aren't even in the right forest, let alone barking up the right tree.

Make sure we get some credit when you turn in your homework.

Oh come on PaulS it wasnt that bad. At least there had been an attempt - a lot better than the usual "urgent need solution" posts.

Besides, the question is mostly simply solved by the program

void setup() { Serial.begin(9600); Serial.println("1 2 3 4 5 6 7 6 5 4 3 2 1"); }
void loop() { }

That ought to get (mis-)credit :slight_smile:

Msquare you should get a job as a phone designer. Some have been caught running optimised code when given a benchmark...

You're not old enough, KeithRB. Tweaking a product to match benchmarks is an old trade trick. 8)

One instance I can think of offhand, is graphics cards for pc in the late 80s. The driver code shipping with the card would detect if a large number of rectangles was being sent. It then stopped updating the display (thus accepting rectangle graphics commands really quickly) until the command stream stopped and then it generated the last 50 it had received. The result: Graphic card X was capable of doing 100000 rectangles a second whilst all others only did 500 or 1200. (Of course, doing only 50 would have shown it was no faster). Benchmark tester and computer magazines started to use displaying excel sheets as a benchmark to stop that particular cheat.

I'm sure cheats have been done before I or computers were born, too.

Msquare:
That ought to get (mis-)credit :slight_smile:

Sorry man... F

Instructions said minimum 2 spaces between numbers.... :wink:

To be more serious for a moment, milo, please look into for loops for this:
http://arduino.cc/en/Reference/For

But if you're just looking for a completed homework program, no problem - here it is:

int main(){init();Serial.begin(9600);uint32_t
l=2105393;char *p=(char*)&l,i=1;while(l&&*p!=
0x30&&i){Serial.print(p);if((p[0]+=i)==55)i*=
-1;}Serial.println();}

Trust me, it works :slight_smile:

No it doesn't! Not for the stated problem.

I get:

1  2  3

??? That's weird.
Are you sure your terminal didn't truncate anything or something?
I'm not even sure how that output would be possible with the given code.

:frowning:

I admit I changed it to use 115200 baud.

Using your exact code I now get:

1

So, "F", I'm afraid.

Better luck next semester.

Add this before the final brace and it works:

Serial.flush();

The generated code calls main and then exit. exit() does this:

00000750 <_exit>:
 750:	f8 94       	cli

00000752 <__stop_program>:
 752:	ff cf       	rjmp	.-2      	; 0x752 <__stop_program>

So it turns interrupts off, and the Serial routines can not empty the serial buffer.

[quote author=Nick Gammon link=topic=195159.msg1441417#msg1441417 date=1382650820]
Add this before the final brace and it works:[/quote]

So, A-? :smiley:

Corrected code for completeness sake:

int main(){init();Serial.begin(9600);uint32_t
l=2105393;char *p=(char*)&l,i=1;while(l&&*p!=
0x30&&i){Serial.print(p);if((p[0]+=i)==55)i*=
-1;}Serial.println();Serial.flush();}

The message timestamps says it took you <= 9 minutes to find the flaw, Nick. Cool 8) (the clue was it managed to do 3 at the higher baudrate, was it?)

By The Way: by now we probably have scared off the OP. Is that a good thing?

int2str:
So, A-? :smiley:

I will assume you jest. :wink:

Gammon Forum : Electronics : Microprocessors : Debugging using SPI/I2C and a second processor
(the clue was it managed to do 3 at the higher baudrate, was it?)

There was that, plus I added debugging code, and most of the numbers appeared, hinting that the serial buffer was not being flushed.

You could have avoided the problem by using setup() and loop() and the code would be smaller anyway.

void setup(){Serial.begin(9600);uint32_t
l=2105393;char *p=(char*)&l,i=1;while(l&&*p!=
0x30&&i){Serial.print(p);if((p[0]+=i)==55)i*=
-1;}Serial.println();}void loop(){}

@OP: We are just fooling around. If you submitted that code I would fail you, for sure.

To the OP (that's you, milosaurus)!

When you say it's not working, what do you mean? Have you set the Serial Window Baud rate to that in your program?

@int2str:

You could have made it slightly more obscure, and saved a few bytes:

void setup(){Serial.begin(9600);uint32_t
l=2105393;char*p=(char*)&l,i=1;while(l&&*p!=
48&&i){Serial.write(p);if((*p+=i)==55)i*=
-1;}Serial.println();}void loop(){}