Void Loop() Will not run, at all.

So the task was to create some light objects, assign each object to an LED light connected to the arduino, and then later on be able to control which light becomes active with a couple of push buttons.

 int *on = 2;


 class Lights{  
 public:
 int identity;
 bool light_Up() /* Returns true if the light is flashing*/{
    while(identity == on){
      pinMode(OUTPUT, identity+3);
      while(true){
     digitalWrite(identity+3, HIGH);
     delay(500); 
     digitalWrite(identity+3, LOW);
     delay(500);};
        return true;}
        return false; };


 Lights(int x) /* Sets the identity and starts the blinking*/{
  this->identity = x;
  light_Up();
 };
};

void setup() {
  // put your setup code here, to run once:
 

pinMode(9, INPUT_PULLUP); 
pinMode(10, INPUT);
Serial.begin(9600);
Serial.println("Serial begin");
Serial.println(digitalRead(9));
 
 for (int i = 0; i<3; i++){
    Lights ptrLight = new Lights(i);   
};
  
}

void loop() { // Nothing in here runs, at all. 
Serial.println(digitalRead(9));
delay(2000);

}

The offending piece seems to be the for loop inside void setup(). If I delete this, then the loop() works totally fine, but I'm just not understanding why?
If I move the for loop to the very top of void setup(), then nothing will run at all, so there's something dodgy about the loop. I mean it works, and I get the correct LED flashing so it is working as intended, but nothing after the loop will run at all.

PS, I apologize for the horrific code. I'm not an expert.

pinMode(OUTPUT, identity+3);

Oops

What is the point of the for loop in setup?

What's the point of the comparison of an int and and int pointer in "light_up"?

If you're still wondering why loop() never runs, consider thiswhile(true){

TheMemberFormerlyKnownAsAWOL:

pinMode(OUTPUT, identity+3);

Oops

What is the point of the for loop in setup?

Huh. I can't do that? I mean, sure it looks horrific, but isn't this valid?

I wanted to create three instances of the "Light" object without having to manually name each one myLight_one, myLight_two, myLight_three... etc.

I've since replaced the for loop with:

Lights myLight_one(0);
Lights myLight_two(1);
Lights myLight_three(2);

And my arduino still doesn't like it.

I've even gone back and amended the following:

 while(identity == on){
      pinMode(OUTPUT, 3);
      while(true){
     digitalWrite(3, HIGH);
     delay(500); 
     digitalWrite(3, LOW);
     delay(500);};
        return true;}
        return false; };

And still no dice!

Light_up() should probably compare the pointer to a pointer, that's a mistake on my part. But even with int on = 2 (ie, comparing int with int) the loop still doesn't execute.

TheMemberFormerlyKnownAsAWOL:
If you're still wondering why loop() never runs, consider this

while(true){

Huh. That was it! Thanks!

Why is that? Arduino can only run one function at a time?

Again

pinMode(OUTPUT, 3);

Why do you think you can jumble things up just to suit you?

another_melody:
Huh. That was it! Thanks!

Why is that? Arduino can only run one function at a time?

Arduino has no operating system. setup() executes once. loop() over and over again. That's all you get.

When you start following the syntax of C++ come back then, when you have problems.

TheMemberFormerlyKnownAsAWOL:
Again

pinMode(OUTPUT, 3);

Why do you think you can jumble things up just to suit you?

.....? I don't follow.

I have LEDs plugged into pins 3, 4 and 5.

Where else am I supposed to learn C++ if not on a forum specifically for enthusiasts. I didn't realise I had to be on the same level as an elitist before I can make an account or buy an Arduino. My bad, I will go somewhere else.

The method light_Up() doesn't make sense. In the first while loop you are comparing an integer to an integer pointer and yes it will actually equal to 2 at some point which causes your next issue: you enter an endless loop. Also, pinMode(OUTPUT, identity+3); should be pinMode(identity+3, OUTPUT);

 class Lights{  
 public:
 int identity;
 bool light_Up() /* Returns true if the light is flashing*/{
    while(identity == on){
      pinMode(OUTPUT, identity+3);
      while(true){
     digitalWrite(identity+3, HIGH);
     delay(500); 
     digitalWrite(identity+3, LOW);
     delay(500);};
        return true;}
        return false; };


 Lights(int x) /* Sets the identity and starts the blinking*/{
  this->identity = x;
  light_Up();
 };
};

Look at all the pinModes in all the examples you've worked through.
Do any of them look like that one?

It doesn't even look like the other pinModes in your own sketch.

Elitism - it isn't for everyone.

another_melody:
.....? I don't follow.

I have LEDs plugged into pins 3, 4 and 5.

Where else am I supposed to learn C++ if not on a forum specifically for enthusiasts. I didn't realise I had to be on the same level as an elitist before I can make an account or buy an Arduino. My bad, I will go somewhere else.

TheMemberFormerlyKnownAsAWOL can be brash; however, there are tutorials and references for configuring and using the pins. You have the parameters in the wrong order for that.

I would suggest starting with a simpler program (one of the examples, actually) that flashes one LED and make sure you understand it and then move forward keeping in mind that Arduino is a very limited platform due to memory and processing constraints; hence, no operating system and single-threaded.

ToddL1962:
TheMemberFormerlyKnownAsAWOL can be brash; however, there are tutorials and references for configuring and using the pins. You have the parameters in the wrong order for that.

I would suggest starting with a simpler program (one of the examples, actually) that flashes one LED and make sure you understand it and then move forward keeping in mind that Arduino is a very limited platform due to memory and processing constraints; hence, no operating system and single-threaded.

Ok thanks. I had started off with just one, everything was working fine until I decided to add more things into the loop, I've been reading documentation and tutorials and configurations for the past two days until I decided to post here.

I didn't realise I had that the wrong way around, or that infinite loops were a bad thing.

Let me explain what some people mistake for "brashness".

You ask a question "what's wrong with my code?"

I provide responses that are formed as a sort of triage.

You can either:
a) smack yourself on the forehead and exclaim " of course, I should have seen that. I just needed a fresh set of eyes"
b) I don't understand, could you explain a little more?
c) go off on one, and complain that no-one understands your approach to programming.

This allows me to formulate how I proceed, and what level of assistance is required, or offered.

Does this code actually compile?

TheMemberFormerlyKnownAsAWOL:
Let me explain what some people mistake for "brashness".

You ask a question "what's wrong with my code?"

I provide responses that are formed as a sort of triage.

You can either:
a) smack yourself on the forehead and exclaim " of course, I should have seen that. I just needed a fresh set of eyes"
b) I don't understand, could you explain a little more?
c) go off on one, and complain that no-one understands your approach to programming.

This allows me to formulate how I proceed, and what level of assistance is required, or offered.

I was trying to be nice :slight_smile:

Proietti:
Does this code actually compile?

Actually it did.

TheMemberFormerlyKnownAsAWOL:
Let me explain what some people mistake for "brashness".

You ask a question "what's wrong with my code?"

I provide responses that are formed as a sort of triage.

You can either:
a) smack yourself on the forehead and exclaim " of course, I should have seen that. I just needed a fresh set of eyes"
b) I don't understand, could you explain a little more?
c) go off on one, and complain that no-one understands your approach to programming.

This allows me to formulate how I proceed, and what level of assistance is required, or offered.

I asked why loop doesn't work, and yes I know the code is bad.

If I had not posted all the "bad" bits that came with it, undoubtedly the next response would have been "We can't help you, post the whole code". It's a catch 22.

Infinite loop = bad would have been a good enough answer, I don't mind constructive criticism but this is straight snarkiness. I received one good post at the beginning and then straight hostility.

Either way thanks to this board I have everything working now

Also yes the code compiled, much to my surprise as well.

When code smells that bad, there could be a number of reasons things go pear-shaped.

The initialisation of an int pointer with the value 2.
What is at memory address 2?
Probably a processor register.
What will happen if I read or write to that register?
Could it produce the symptoms described? Possibly.

What is the value of "OUTPUT"?
I don't know, but it is probably also a valid pin number, possibly even one of the serial I/O pins.
What will happen if I fiddle with that pin's associated DDR in pinMode?
Could it produce the symptoms described?
Possibly.

If you don't like terse responses, you picked the wrong hobby - the compiler doesn't give a rat's about your feelings, and it doesn't even see all the problems in your code.