Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:17+00:00 2026-05-26T01:20:17+00:00

I’m programming something with an Arduino, which I believe uses C as the base

  • 0

I’m programming something with an Arduino, which I believe uses C as the base language. I have this loop:

int i = 0;

void loop()
{
    //Set The output pin to the MIDI note number - 60.
    int pinNumber = noteNumber - 60;

    if (midiMessage == 144) {
        if (velocity == Triggered) {
            registerWrite(pinNumber, HIGH);
            i = pinNumber;
        }
    }

    if (midiMessage == 128) {
        if ((i % 8) == (pinNumber % 8)) {
            if (velocity == Stopped) {
                registerWrite(pinNumber, LOW);
            }
        }
    }
}

I know the above code doesn’t work right, but what I’m trying to do is assign the i variable if the first condition is met if (velocity == Triggered), and then be able to use it when the midiMessage == 128 condition is met.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:20:18+00:00Added an answer on May 26, 2026 at 1:20 am

    i is a global variable, meaning that it’s initialised to 0 (explicitly) when your program starts, and then can be changed by any code that can see it.

    So, if your question is:

    Will it maintain its value between different calls to the loop() function?”

    (and it seems to be, based on the comments you left in the question) then the answer is yes.

    In other words, when you enter loop() with midiMessage equal to 144, it will set i to pinNumber (noteNumber - 60).

    Then the next time you enter loop() with midiMessage equal to 128, it will use the current i (whatever it was last set to).

    Of course, if you enter loop() with midiMessage equal to 128 before you call it when midiMessage equals 144, the value of i will be zero.


    If that declaration of i was inside the loop() function, then it would be re-initialised each time the function was called, but that’s not the case here.

    Of course, good programming practice would suggest that you should put it inside the function if that’s the only place you use it (this may not be the case).

    This restricts its “visibility” (a) to that function so that there’s no chance other code may use it inadvertently (this is especially a problem for a variable called i – you may want to think of a better name for it).

    To restrict its visibility like that while still allowing it to exist between function calls, you would use something like:

    void loop (void) {
        static int persistentVar = 0;
        :
        :
    }
    

    This still initialises it once, at program startup (even though it’s inside the function) and keeps its value in between different invocations of loop(). It also stops code from outside of that function from “seeing” the variable.


    (a) Note that “visibility” as used here is not the term the ISO standard uses. I use that term for my introductory coding classes since “visibility” is easier understood than “scope”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.