Problem with getting function too work

Hi, i'm new to Arduino, and this programming language. I`m trying too learn how too use a function.

It looks like i cant change the integer "t". Ive done some researche and the try and failed method, without luck. It works perfectly directly in void loop.

Does anyone know whats wrong with my program? Probably something obvious, but i`m not that good yet . But im getting there :smiley:

unsigned long previousmillis = 0;
unsigned long currentmillis = millis();
int t = 0;


void setup () {
  pinMode (13, OUTPUT);
  Serial.begin (9600);
  digitalWrite (13, LOW);
  
}

void blinkLED (int blinkpaa, int blinkav) {
  
  unsigned long currentmillis = millis();
  Serial.println (currentmillis);
  
  if (currentmillis - previousmillis >= blinkav & t==0) {
    previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    int t = 1;
  }
  if (currentmillis - previousmillis >= blinkpaa & t==1) {
    previousmillis = currentmillis;
    digitalWrite (13, LOW);
    int t = 0;
  }
}



void loop () {
  
  blinkLED(1000,1000);
  
}
[code]

[/code]

Which t are you trying to change the value of? You've defined three different variables named t, at three different addresses.

hmmm... maybe thats the problem? I thought i was changing the one variable named "t".

I thought i was changing the one variable named "t".

By defining two more?

  if (currentmillis - previousmillis >= blinkav & t==0) {
    previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1; // Diddle with the ONE global variable.
  }

When you very first declare a variable you need the int part like this

int t = 0;

Everywhere else you use it leave the int part off:

t = 1;

If you put the "int" there again then you are declaring a second variable with the same name and it comes down to scoping rules.

shit, posted the wrong code :blush: I tried the int in desperation.... It does not work without the int neither

Delta_G:
When you very first declare a variable you need the int part like this

int t = 0;

Everywhere else you use it leave the int part off:

t = 1;

If you put the "int" there again then you are declaring a second variable with the same name and it comes down to scoping rules.

okay, so this is kind of embarrassing, but it works now :blush: Thank you for the help :smiley:

One quick question, does it matter where in the program the function is?

shit, posted the wrong code

It’s never too late to post the right code.

does it matter where in the program the function is?

No. In a regular C program, it does but the Arduino IDE creates prototypes for you behind the scenes so you don't have to worry about it.

PaulS:

shit, posted the wrong code

It's never too late to post the right code.

I dont what I did different, but the code works now :slight_smile: I`ve been struggeling for hours and suddenly it works :stuck_out_tongue:

But here is the working code

const int melding = 1000;
unsigned long previousmillis = 0;
unsigned long previousmillis2 = 0;
unsigned long currentmillis = millis();
int t = 0;
int nr = 0;

void setup () {
  pinMode (13, OUTPUT);
  Serial.begin (9600);
  digitalWrite (13, LOW);
}

void loop () {
  
  blinkLED(1000,1000);
  
  unsigned long currentmillis = millis();
  
  if (currentmillis - previousmillis2 >= melding) {
    previousmillis2 = currentmillis;
    Serial.println (nr);
    nr = nr + 1;
  }
  if (nr > 10) {
    nr = 0;
  }
}

void blinkLED (int blinkpaa, int blinkav) {
  
  unsigned long currentmillis = millis();
   
  if (currentmillis - previousmillis >= blinkav & t==0) {
    previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1;
  }
  
  if (currentmillis - previousmillis >= blinkpaa & t==1) {
    previousmillis = currentmillis;
    digitalWrite (13, LOW);
    t = 0;
  }
}
[code]

[/code]

wildbill:

does it matter where in the program the function is?

No. In a regular C program, it does but the Arduino IDE creates prototypes for you behind the scenes so you don't have to worry about it.

OK, thanks :smiley:

and suddenly it works

For some definition of work, I suppose.

  if (currentmillis - previousmillis >= blinkav & t==0) {
    previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1;
  }
  
  if (currentmillis - previousmillis >= blinkpaa & t==1) {

I can't see how bitwise ANDing true and true, true and false, false and true, or false and false is what you want.

Logical ANDing (&&) is a different story...

PaulS:

and suddenly it works

For some definition of work, I suppose.

  if (currentmillis - previousmillis >= blinkav & t==0) {

previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1;
  }
 
  if (currentmillis - previousmillis >= blinkpaa & t==1) {



I can't see how bitwise ANDing true and true, true and false, false and true, or false and false is what you want.

Logical ANDing (&&) is a different story...

As said befor, im new to this and i`m still learning. I needed something to decide which "if" to go with. So this was the best I could come up with my limited knowlegde of different commands and functions :slight_smile:

If you have improvements or other solutions i`d be happy to know :smiley:

You need to better understand the concept of scope. Take a look at reply #2 at:

and then ask yourself what happened to the scope of "int t" before and after your code changes.

jonaskluver:

PaulS:

and suddenly it works

For some definition of work, I suppose.

  if (currentmillis - previousmillis >= blinkav & t==0) {

previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1;
  }
 
  if (currentmillis - previousmillis >= blinkpaa & t==1) {



I can't see how bitwise ANDing true and true, true and false, false and true, or false and false is what you want.

Logical ANDing (&&) is a different story...

As said befor, im new to this and i`m still learning. I needed something to decide which "if" to go with. So this was the best I could come up with my limited knowlegde of different commands and functions :slight_smile:

If you have improvements or other solutions i`d be happy to know :smiley:

What he's (cryptically) trying to tell you is that & and && are different operators. & is a bitwise operator and not what you want here. && works with boolean values and is the thing you need here. Look in the reference section for more information

Delta_G:

jonaskluver:

PaulS:

and suddenly it works

For some definition of work, I suppose.

  if (currentmillis - previousmillis >= blinkav & t==0) {

previousmillis = currentmillis;
    digitalWrite (13, HIGH);
    t = 1;
  }
 
  if (currentmillis - previousmillis >= blinkpaa & t==1) {



I can't see how bitwise ANDing true and true, true and false, false and true, or false and false is what you want.

Logical ANDing (&&) is a different story...

As said befor, im new to this and i`m still learning. I needed something to decide which "if" to go with. So this was the best I could come up with my limited knowlegde of different commands and functions :slight_smile:

If you have improvements or other solutions i`d be happy to know :smiley:

What he's (cryptically) trying to tell you is that & and && are different operators. & is a bitwise operator and not what you want here. && works with boolean values and is the thing you need here. Look in the reference section for more information

Okay, so I think i got the differens between "&" and "&&". The "&" is on a "binary level" , and "&&" is what I meant to use. I guess the same goes for OR, "|" and "||".

I guess it worked in my code because, true & true = true. And thats why IF were fulfilled?

Here are some examples of my idea of what this is:

  • AND
void setup () {
  Serial.begin (9600);
  int a = 13;         // 1101  1*2`3(=8) + 1*2`2(=4) + 0*2`1(=0) + 1*2`0(=1) = 13
  int b = 11;         // 1011  1*2`3(=8) + 0*2`2(=0) + 1*2`1(=2) + 1*2`0(=1) = 11
  int c = a & b;      // 1001  1*2`3(=8) + 0*2`2(=0) + 0*2`1(=0) + 1*2`0(=1) = 9
  Serial.println (c);
}

void loop () {
  // Ikke bruk for program
}
  • OR
void setup () {
  Serial.begin (9600);
  int a = 13;        // 1101 1*2`3(=8) + 1*2`2(=4) + 0*2`1(=0) + 1*2`0(=1) = 13
  int b = 11;        // 1011 1*2`3(=8) + 0*2`2(=0) + 1*2`1(=2) + 1*2`0(=1) = 11
  int c = a | b;     // 1111 1*2`3(=8) + 1*2`2(=4) + 1*2`1(=2) + 1*2`0(=1) = 15
  Serial.println (c);
}

void loop () {
  // Ikke bruk for program
}