***Solved*** After filtering out everything. This shoud work, but it does'nt

Hi you guys,

I encountered a bug in my software, which I narrowed down to just a few lines of code.
I'm quite new in this but I'm a quick learner. Especially the basics.

byte PreKar = 1; 
byte Kar = 0; 
 


void setup() 
 {
  Serial.begin(9600);
 }


void loop() 
 
 {
  bitWrite (PreKar, Kar, 1);
  delay(1500);

  Serial.println (PreKar);
  Serial.println (Kar); 
  Kar++;
  if (Kar = 64)
   {Kar = 0;}
 }

The thing is I have not enough experience with "bitwrite" I guess.
My intention is to have a Byte, named "PreKar" and change the individual bits it contains.
If I understand the explanation on the website correct, I'd name the Byte, the position, and the value.

For some reason that doesn't work.
In this matter I wrote a simple routine which increases the variable "Kar" so every 1,5sec. the next bit is changed to 1, so over time the whole byte should be turned 1.
But to keep it manageable I created a reset which resets things, so I can monitor its behavior.

For some reason, I keep on seeing only the 1 and a 0 on the next line because "Kar" does not increases.

Please do not ask me for my real intentions so one can find a better solution for that. :frowning:
That is not the way for me to learn.
I'd just want to understand that bitWrite thingy. :confused:

It is not

  if (Kar = 64)

but

  if (Kar == 64)

There is a difference between assignment and comparison.

  if (Kar = 64)

This sets Kar to 64. Is that what you meant to do or did you mean to compare Kar with 64 ?

UKHeliBob:

  if (Kar = 64)

This sets Kar to 64.

...and very shortly after, to zero.

  bitWrite (PreKar, Kar, 1);

This instruction sets the bit number ‘Kar’ (counting from right to left) of the variable ‘PreKar’ to 1.

Prekar is a byte, so it contains only 8 bits. You declare it with the initial value of 1, so the binary value of PreKar is : 00000001
Then you set Kar to 0 and call bitWrite to set the bit number 0 to 1, i.e. the rightmost bit. But it’s already 1 so there is no change.

Then you increase Kar, bitwrite sets the second bit of PreKar to 1. PreKar becomes equal to 00000011.
And so on, until reaching the time when Kar is equal to 7. At that moment, bitWrite sets PreKar to 11111111.

But you seem to want to continue after 7, until 64 in fact. So you ask bitWrite to set bit number 9 of a variable that only has 8… That does not make sense, and depending on the way bitWrite is written, different things may happen. bitWrite can simple return without changing the bit. Or it can change the first bit of the memory space next to the one where PreKar is stored. Or anything else…

So you should compare Kar to 8, not to 64:

  if (Kar == 8) Kar = 0;

This sequence of code in repeated endlessly, as it is in the loop. But you don’t need that: one time is enough. Do it in the setup;

void setup() {
  Serial.begin(9600);

  byte PreKar = 0; // 0 instead of 1 ?
  byte Kar = 0; 
 
   while (Kar <8) {
   bitWrite (PreKar, Kar, 1);
   delay(1500);
   Serial.println (PreKar);
   Serial.println (Kar); 
   Kar++;
 }

void loop () {}

Tx guys!

The " if (Kar = 64) " is yet another Mindset I have to work on. :confused: I used to work with other languages where this was common.

I'll fix that one.

The other thing supprised me!
I thought a Byte can have a value of 64, beeing 01000000 in binary.

So... If I'm to change ie the 3th bit into 1 (00000100), Kar would not change that to 4 in Decimal??

In what stage is the situation that is Kar a full 255 Byte, if it only covers values from 0 to 7?

I thought a Byte can get its 8 individual bits changed, giving it the value of the position of that bit.

I'll look into it!
Tx so far. :slight_smile:

You’re confusing bit position (power of two) with value.
A byte can hold the value 64, but cannot represent 264

FTMZ:
Tx guys!

The " if (Kar = 64) " is yet another Mindset I have to work on. :confused: I used to work with other languages where this was common.

I'll fix that one.

When working in a new language its worth going through the language description and write yourself a
cheat-sheet of all the things that are new/different that might trip yourself up, and consult it when writing
in the new language.

MarkT:
When working in a new language its worth going through the language…

I got that, Mark. It is just a fraction of a bigger program, with only the bitWrite problems.
True… I missed that “if” statement, but that is not the main problem.
I try to understand the BitWrite instruction.

AWOL:
You’re confusing bit position (power of two) with value.
A byte can hold the value 64, but cannot represent 264

OK, but does that mean I can do:

bitWrite (KarPos, 7, 1)

And it makes the MSB of that KarPos Byte 1?

So… (I made a mistake in my previous reply AND in my opening post… :frowning: ) the Byte “KarPos” is then 128?

Then I seem to get it.
The first is the variable (in my case a Byte) the second variable is a value of 0 to 7, and the last number is the 1 bit value I’d like to give to that bitposition.

Gottit…
I’ll check it out this evening. :wink:

Thanks for your help People!

Correcting the noticed errors did the trick.