Pages: 1 [2] 3 4   Go Down
Author Topic: loops  (Read 3286 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include "IRremote.h"


int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

void setup()  {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
}



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ;
        } 
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(11, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ;
        } 
  lastRun = millis();
    }
}

void loop() {
 // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B77887) {
     num = 1  ;
    }
   
  do_task(num);
 
 }
}

void do_task(int num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

and now i get the folowing errors (see image)


* error12_04.png (163.52 KB, 1600x900 - viewed 17 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The error messages are fairly self-explanatory - you haven't told the compiler about the variables you are using, before you use them.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and how do i do that?
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lastRun needs to be a long, so declare it at the top of your sketch
Code:
long lastRun;
  As for 'results', I don't see where that's being defined either.  That's something you need to figure out from the IR library you're using.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i thanks for tha code with lastrun....
i solved that results thing to now only num is the problem, the error says " 'num' was not declaired in this scope" and      num = 1  ; is marked and "do_task(num)" too
how declair i this (and wher)?
Code:
#include "IRremote.h"
#include "IRremoteInt.h"




long lastRun;
int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);
decode_results results;

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

void setup()  {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
}



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ;
        } 
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(11, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ;
        } 
  lastRun = millis();
    }
}



void do_task(int num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

void loop() {
 // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B77887) {
     num = 1  ;
    }
   
  do_task(num);
 
 }
}
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL already responded with one solution, declaring it in the call itself with
Code:
void do_task(int num)
or you can declare it like you do any other variable already, at the top of your code with simply
Code:
int num;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

then... i am sorry
becaus i don't understaand what you, and AWOL are saying
i dont dow How i declair it in that, do you meen that instaed of putting the decoded values in cmd_one in do_task(num)
if zo how can i make the difrence between al the difrent ir data codes?
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void cmd_one() {
  // do one LED sequence here
  analogWrite(9, brightness);
  brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    } 
    lastRun = millis();
  }
}
Something else to note: this sequence changes the LED brightness every 50 cycles.So let's do some calculations:
LED brightness goes from 0 to 255.  fadeAmount is set to 5.  So that's 255/5 = 51 steps to go from 0 to 255.
Steps happen every 50ms, so that's 2550ms (2.55 seconds) to go from 0 to 255 brightness.  Or a little over 5 seconds to go from 0 to full brightness and back down to 0.

Given that tasks aren't precise, it will be a bit more than that, meaning the task won't run at exactly 50ms each time.  it might run at 53ms, or 57ms ... everything takes time in Arduino land.  So, if you're okay with a 5+ second fade on and back off, then you're good to go.  If not, then lower the 50ms compare statement to a smaller number and the fade will go faster:
Code:
if (millis() - lastRun > 50) // <-- change that 50 to a lower number to go faster, or larger to go slower
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
then... i am sorry becaus i don't understaand what you, and AWOL are saying
You've already had help defining another variable - what's the problem?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

then... i am sorry
becaus i don't understaand what you, and AWOL are saying
i dont dow How i declair it in that, do you meen that instaed of putting the decoded values in cmd_one in do_task(num)
if zo how can i make the difrence between al the difrent ir data codes?

Exactly the way either options are written.  AWOL method
Code:
void do_task(int num)  // <-- that declared 'num' as an INT right in the call
or you can do it the other way, which is to declare the variable like you are already doing for 'lastRun' and 'RECV_PIN', just add another line that says
Code:
int num;
But do one or the other, don't do both.  but you write them in exactly as they're written here.  Nothing to change, nothing to substitute.

Groetjes.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
then... i am sorry becaus i don't understaand what you, and AWOL are saying
You've already had help defining another variable - what's the problem?
I think the problem here is that he's never programmed before and I can certainly understand the frustration.  I deal with kids in a teaching environment too and see this every day.  I show them how to do something and 5 seconds later they ask the exact same thing.  Patience is a virtue. smiley  I don't mind explaining something a second time ... or third ... or ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thats right it's the first time that i programmed
now, back to the prgramme, i have no errors but i did used the 2 at the same time
AND now it doesn't matter what butten i push on my remote my led goes one and of when i push a button again....
Code:
#include "IRremote.h"
#include "IRremoteInt.h"


int num;


long lastRun;
int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);
decode_results results;

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

void setup()  {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
}



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
    //  if (brightness == 0 || brightness == 255) {
          fadeAmount = +fadeAmount ;
      //  } 
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
  if (millis() - lastRun > 50) {
     // if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ;
       // } 
  lastRun = millis();
    }
}



void do_task(int num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

void loop() {
 // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B73AC5) {
     num = 1  ;
    }
   
    if (results.value==0x8B77887) {
      num = 2 ;
    }
  do_task(num);
   irrecv.resume();
 }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do your debug prints tell you?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pleeze do not kill me
because i dont know what "debug prints" are/or how i find them...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You've got Serial.println statements in your sketch. They're there to show what your program is doing, in the serial monitor.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2] 3 4   Go Up
Jump to: