Writing a Function

I am current trying to write a function to shift a single bit into the PORTB register only for bits 0 1 2 & 3.

void forward()
{
 PORTB = 1 << PORTB;
int i;
for(i=0;i<2000;i++);
}

void reverse()
{
 PORTB = 1 >> PORTB;
int i;
for(i=0;i<2000;i++);
}

As the ATtiny85 only has PORTB 0,1,2,3,4,5.
So my intended pattern is.

forward();
0001
forward();
0010
forward();
0100
forward();
1000
reverse();
0100
reverse();
0010
reverse();
0001
reverse();
1000

How do i force it to only push 1 into bits 0,1,2 & 3 only & not the full 6bits of the register?
Can this be done in a single instruction?

Have a look at the bitwise operators, in particular the bitwise or operator

Bitwise OR (|)

The bitwise OR operator in C++ is the vertical bar symbol, |. Like the & operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0. In other words:

0 0 1 1 operand1

0 1 0 1 operand2

0 1 1 1 (operand1 | operand2) - returned result Here is an example of the bitwise OR used in a snippet of C++ code:

int a = 92; // in binary: 0000000001011100 int b = 101; // in binary: 0000000001100101 int c = a | b; // result: 0000000001111101, or 125 in decimal.

Read this before posting a programming question

Code tags, please.

http://arduino.cc/en/Reference/BitSet

for(i=0;i<2000;i++);

The purpose of this is? …

Waste some time.

Waste some time

Why waste it when you could use it ? What does wasting time achieve , and how much time do you think that you are actually wasting ?

Why waste it when you could use it ? What does wasting time achieve , and how much time do you think that you are actually wasting ?

The relevance to the original post?

About 5 minutes answering your line of questioning!

Please accept my apologies for being helpful and answering your direct question in reply #1, but I am still intrigued as to why you want to waste time.

You can waste some cycles before a function, during or after a function.
Its up to the end user to determine which is the best method, i have determined it to be when the function is required.

My question pertained to the ability to write to the lower 4bits of PORTB using a SHIFT.

DDRB = B00001111;

States to set PB0-PB3 as outputs.

When i use;

PORTB = (1 << PORTB);

It writes 00000001, 00000010, 00000100, 00010000 … 00000111
1000 was not an omission, for some reason it skips bit 3 writes to bit 4, writes bit 0 1 & 2

Therefore i need to confine to the lowest 4 bits.

JB_AU: Waste some time.

In fact it will waste no time at all, the compiler sees that there is nothing in the loop and so removes it from your code.

So your saying

void setup()
{
DDRB = B00001111;
}

void loop()
{
forward();
}

void forward()
{
 PORTB = 1 << PORTB;
int i;
for(i=0;i<2000;i++);
}

is no different than

void setup()
{
DDRB = B00001111;
}

void loop()
{
 PORTB = 1 << PORTB;
}

?

So your saying...is no different than...

Exactly.

Would it not be less cryptic, to say "You can not do that in a function". Instead of enquiring why you did that, the purpose of it, what you think it does. Or is it more meaningful to play "Can you guess the number i am thinking", and after getting it wrong 100 times, stating how far away from the number you where ?

This portion was quite unhelpful to me, trying to learn how to write a function.

To the viewer, i feel as though their is no point to asking a question unless you already know some if not all of the answer prior to asking, therefore it would be less cryptic to research for a conclusive answer with the little facts you possess.

Would it not be less cryptic, to say "You can not do that in a function".

But, you can do that. It just doesn't make sense, since the compiler will see that the code accomplishes nothing, and remove it.

Asking why you added the code was to make YOU think. If there was a rational explanation, then you could have offered that explanation, and we could/would have explained what erroneous assumptions you were making.

and we could/would have explained what erroneous assumptions you were making.

Which is exactly what we did. Why was this not useful?

JB_AU: So your saying ... is no different than ... ?

Why ask, when you can find out for yourself? Isn't that the way of learning stuff?

First sketch:

Binary sketch size: 488 bytes (of a 32,256 byte maximum)

Second sketch:

Binary sketch size: 488 bytes (of a 32,256 byte maximum)

Clearly the compiler has optimized away that loop.

JB_AU: Waste some time.

Your reply isn't helpful. Why "waste" time? If you want, say, 500 mS to elapse, why not reply: "I want to wait 500 mS"?

And then we might reply, why not type:

delay (500);

JB_AU: Would it not be less cryptic, to say "You can not do that in a function".

Yes you can do that in a function. You just did, didn't you?

Look, read this:

http://www.gammon.com.au/forum/?id=12153#tip5

Don't try to out-think the compiler

If you want to delay for x milliseconds, explicitly code that. My question was intended to prompt some thinking on your part.

At only one point there was a hint that something was wrong

In fact it will waste no time at all, the compiler sees that there is nothing in the loop and so removes it from your code

This was interpreted by ME to mean, if the function is not in the

void loop()
{
forward();
}

It is ignored.

Not

int i;
for(i=0;i<2000;i++);

Is removed from the function

void forward()
{
 PORTB = 1 << PORTB;
int i;
for(i=0;i<2000;i++);
}

At the point when i stated

You can waste some cycles before a function, during or after a function.

Why did someone not ask “How do you waste cycles in a function?”

Here are the following assumptions i make or have made.

Those of us learning, assume the more professional programmers can see errors within our code before we can.

It is assumed socially, that if a person is going to or is making an err, to let them know.

Professionals in almost any area of expertise, except Psychology, make the assumptions that , beginners in their field are like minded.

Professionals (Professors) in a field are like minded.

Assuming that all people think alike, is the one assumption all professionals make were noted, therefore any advancement in any industry is thawted by these assumptions.

P.S. I miss the, someone has just replied to your topic, warning!

int i;
for(i=0;i<2000;i++);

In your original code you might have meant:

int i;
for(i=0;i<2000;i++)
  flashLED ();

Who knows what you wanted to do 2000 times? That’s why I asked.

Imagine for a moment you had written:

void forward()
{
int i;
for(i=0;i<2000;i++);
 PORTB = 1 << PORTB;
}

You (we) might be excused for thinking you were planning to move 1 to that bit 2000 times and had accidentally put a semicolon there. It’s been done plenty of times before.

Close your eyes.
Inhale.
Count to 5.
Exhale.

Look at post #1 , before completely going off topic.

The;

int i;
for(i=0;i<2000;i++);

Got stuck like a thorn, in all the professional programmers, you became fixated to this err.
Only one person objectively tried to answer the original questions, my thanks to you. (sincerely)

What where the original questions?

My apologies to all, i will admit, i should have elaborated more like this.

void loop()
{
  duration = pulseIn(pulsePin, HIGH);
  if (duration < 6000)
  {
    forward();
    mydelay(2000);
  }
  if (duration > 6000)
    backward();
  mydelay(2000);
}

void mydelay(int a)
{
  int i;
  for(i=0;i<a;i++);
}

I was looking for specific answers to specific problems i could not solve.
I was not trying to beat the compiler.

Close your eyes.
Inhale.
Count to 5.
Exhale.

Why? You are the one with the high blood pressure/thin skin.

Got stuck like a thorn, in all the professional programmers, you became fixated to this err.

No. We asked you to clarify your intent. You got all bent out of shape over that.

void mydelay(int a)
{
  int i;
  for(i=0;i<a;i++);
}

The compiler will STILL see this a useless. It will then see that the whole function call is useless. You have to actually do something in the loop.