Help my arduino code

Makes sense.

Worse, 'cnt' at least, is also misleading. Because it reads as "count". But it is not being used to count anything.

It's not your professor's code.
He only gave me conditions.
The inefficient code is, sadly, my own.

I guess it's because I'm a terrible language learner and I learned it from the internet without a textbook.

Well, I'm sorry for the misjudgement. However, it means that you are basically copy-pasting or copying patterns without much understanding. That won't get you very far.

Specifically, you made extensive use of arrays, but didn't leverage them to do the useful code factoring that they are designed for. Any of the code you took it from, would also contain that. So you need to start some organized, step by step learning and stop trying to guess how things work.

#define LED_PIN_1 11
#define LED_PIN_2 10
#define LED_PIN_3 9

int led[NUM_BUTTONS] = {LED_PIN_1, LED_PIN_2, LED_PIN_3};

I think so, too.
If it's an excuse, it's because there are only so many classes you can take.
I was forced to take a course.
Originally, I should have slowly learned from the bottom up.
But because of the limited number of classes, I made the mistake of taking a class and trying it out.

Well, in my school, that was common, but you could exit without a grade if you did it in the first 2 weeks of the course. You should talk to the prof or proctor about it.

I'm surprised they let you in without the prerequisite studies...

But the real question is, are you willing to learn? To do that, you have to pause the questions here and go do some research. Because, the basic answers to your question have already been posted in this thread.

The two main topics you need to investigate are indicated in replies #2 and #3.

Once you take this one, you're stuck with it.

There is no textbook, example code, or explanation of the code, so it was just thrown at us like the conditions I posted and we had to do it. It's not easy because it's my first time using Arduino in such a state.

They didn't even tell me the circuit settings on the board, so I had to find it and learn it.

The Arduino community also came to me because of this class.

Every time they give you an assignment, but they don't tell you anything, so you have to do it by yourself for a short period of time, so it's a vicious circle.

I didn't realize that the first students' evaluation was so bad.

The problem pointed in post #2 I have fixed in #22.

Thank you for your help so far.
I'm very tired from lack of sleep.
I'll have to get up and try again, one step at a time. Thank you so much.

For a beginner the task is quite a challenge.

But if you are willing to learn, don't give up! People in the forum can help you, and the better the more they know about your situation.

If you want to go on I recommend that you start with solving parts of the task:

  • Find out about "debouncing" of buttons; that's absolutely necessary, because you want to read them quickly: You will run into that problem when you start to avoid delays() ...
  • Create a function that switches the red LEDs only but does not use delays. Once it is working you can use the principle for the blue LEDs and - with some modifications - with the buzzer.
  • Integrate (put together) the function with one button and the red LEDs. When it works expand the sketch to switch the blue LEDs and the buzzer.

I would also recommend that you create a sketch first where all three parts can be switched on/off independently .

Once this is done you can start to integrate also the sequence 1 On -> 2 On -> 3 On -> 3 Off -> 2 Off -> 1 Off

ec2021

Yeah I figured that out

const int PushButtonPin[] = {12, 7, 3};
const int LedPin[][3] = {{11, 10, 9}, {6, 5, 4}};
const int BuzPin = 13;
bool buttonReleased[3];
unsigned char state[3];
unsigned char steps[3];
const unsigned long interval[] = {300UL, 300UL, 300UL};
unsigned long timeStamp[3];


void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 3; i++) {
    pinMode(PushButtonPin[i], INPUT_PULLUP);
    pinMode(LedPin[0][i], OUTPUT);
    pinMode(LedPin[1][i], OUTPUT);
  }
  pinMode(BuzPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < 3; i++) {
    if (digitalRead(PushButtonPin[i]) == LOW && buttonReleased[i]) {
      state[i] ^= 1;
      if (state[i] > 0) timeStamp[i] = millis();
      buttonReleased[i] = false;
    }
    else if (digitalRead(PushButtonPin[i]) == HIGH) buttonReleased[i] = true;
    if (state[i] > 0) {// sequence is on
      if (millis() - timeStamp[i] >= interval[i]) {
        timeStamp[i] += interval[i];
        if (i == 2) {//buzzer
          steps[i] ^= 1;
          if (steps[i] > 0) digitalWrite(BuzPin, HIGH);
          else digitalWrite(BuzPin, LOW);
        }
        else {// red or blue LED
          if (steps[i] >= 3) steps[i] = 0;// (steps[i] == 3) ? all red or all blue = off
          else steps[i]++;
          for (int j = 0; j < 3; j++) {
            if (j = steps[i]) digitalWrite(LedPin[i][j], HIGH);
            else digitalWrite(LedPin[i][j], LOW);
          }
        }
      }
    }
    else {// sequence is off
      for (int j = 0; j < 3; j++) {
        if (i == 2) digitalWrite(BuzPin, LOW);
        else digitalWrite(LedPin[i][j], LOW);
      }
    }
  }
}

didn't realize this was an assignment. thought you were just a vet (thanks) trying to learn to code

after reading some of the comments, i wonder what kind lectures are given in a coding class. two things are needed:

  • one to learn coding concepts and
  • the syntax of the language.

i wonder what kind of lectures there were on coding concepts.

my analysis of the code assuming the 3 possible sequences can be active simultaneously and are synchronized

  • seems that each activity is conditional on the change of state of a button and shouldn't be. the change in state of a button should toggle whether the activity is active, but exercising the activity should be independent of the button state. therefore recognizing that a button is pressed and that an sequence is active need to be independent

  • recognizing a button pressed also needs to be debounced (switch contacts make contact multiple times) to insure that a button pressed is recognized.

  • don't understand the purpose of buttonReleased, i thing butLast makes more sense. recognizing that it has changed and the it is LOW indicates a button being pressed.

  • i don't see any way for the state to change in the while (state [X] == true) loops meaning that the code will be trapped in them (i,e., infinite loop).

  • while delay (300) could be used to time the events, it prevents monitoring the buttons. a "timer" using millis() to recognize that 300 msec has elapsed is the conventional solution

unsigned long msec = millis();
if (msec - msecLst >= 300) {
    msecLst = msec;
    ...     
}
  • the state[] of each of the three activities can checked when the timer expires (i.e. >= 300) the "..."

  • an idx value for each of the led sequences can be maintained to determine the next LED to turn on (or simply toggle) as well as when to turn off all the leds (i.e. >= 3) to repeat the sequence

  • the buzzer is somewhat different but can be handled in a similar way with the timer (not sure if tone() blocks, which can be a major issue)

  • and finally, there's the need to turn off all the LED and buzzer when the state toggles and become non-active

this is an interesting assignment. again, curious about the lectures or if the professor expects student to research possible solutions using google. (problem is finding "good" solutions).

would make sense to start with a single led sequencer. no doubt and 2nd LED sequence could be added and it doesn't need to be the same # of leds.

i'm curious if this is an assignment on multi-tasking or just non-blocking code. a lot can be done without a multi-tasking OS

Hi,
What is the course?

What is the subject?

Can you use any of the schools/university resources?
Can you ask lecturers advice?

Has your class taught you the basics of Arduino programming?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Me too.

I would start to design structures containing all relevant data to run a method to generate the desired actions.

Have a nice day and enjoy coding in C++.

Welcome to the club ... :wink:

I have the same observation. Either the TOs try to have an easy live and let others do the "dirty work of coding" or ... see the quote from another thread:

The teacher I have for this class is pretty laid back and he is also learning how to use arduino and things like it.

The conditions under which some students are given assignments seem - let's say -- interesting ...

ec2021

@adspig - now i'm curious what the previous assignments were. presumably building blocks leading up to this one

sum-of-two-integers-and-led
May be this one was his previous assignment