element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Change multiple values in a boolean array?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 4 replies
  • Subscribers 393 subscribers
  • Views 792 views
  • Users 0 members are here
Related

Change multiple values in a boolean array?

l1t7l3ph0o7
l1t7l3ph0o7 over 10 years ago

I can't seem to figure this one out,

the scenario is, I have two shift registers connected to my arduino and 16 leds connected to them.

I have a boolean array of 16, then I wrote a function that writes the array to the shift registers. Thats what the writereg(); is

 

I can do neat things like:

for (int i = 1; i<16; i++){

registers[i] = high

}

writereg();

and one led will light up one after the other

 

But is there a way to light up specific multiple leds without a for loop in a single line of code?

The only thing I can think to do is:

registers[1] = HIGH;

registers[4] = HIGH;

registers[10] = HIGH;

registers[16] = HIGH;

writereg();

it would be neat if I could do something like this:

registers[1,4,10,16] = HIGH;

writereg();

anyone know, just curious if there is a less caveman approach to what I'm doing

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +1
    The answer to your question is quite simple Direct port manipulation The ATMEGA328 has 3 sets of ports, A, B and C, not all pins of each port are available but it is basically laid out like this B (digital…
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    The answer to your question is quite simple

     

    Direct port manipulation

     

    The ATMEGA328 has 3 sets of ports, A, B and C, not all pins of each port are available but it is basically laid out like this

    • B (digital pin 8 to 13)
    • C (analog input pins)
    • D (digital pins 0 to 7)

    there are 3 registers per port, the DDR register (Data Direction Register), the the Port Register (Sets the pin high or Low if it is an output) and the PIN register that reads the port

     

    there are some pins that you cant use, for instance Port B pin 6 and 7 are used by the XTAL to not available to you

     

    those ports and pins you can use are directly controllable like this

     

    DDRD = B11111110; // sets Arduino pins 1 to 7 as outputs, pin 0 as input, where with the old method it took 7 commands, this takes 1

    DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs, this effectively reads the port, or's it with the mask and sends it out again to set the direction

    // without changing the value of pins 0 & 1, which are RX & TX and if you want to keep using serial,Read or serial.Write, leave these pins alone

     

    now you can control the port set in one command also as follows

    PORTD = B10101000; // sets digital pins 7,5,3 HIGH, you can do the same with the other ports too

     

    you can of course do way more than this with direct port manipulation but this should et you started, mastering boolean algebra will help here to make very efficient code

     

    for a fuller description of this see this link on the Arduino web site Arduino - PortManipulation

     

    Hope this helps, if you need more help, please ask

     

    Peter

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to Robert Peter Oakes

    I'm not sure you understood my question, Or maybe I'm just am having trouble understanding your answer.

    It seems to me that port manipulation is useful for controlling the pins on my arduino, I'm attempting to control pins on two 74hc595 shift registers attached to my arduino.

    am I missing something here?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to Robert Peter Oakes

    I'm not sure you understood my question, Or maybe I'm just am having trouble understanding your answer.

    It seems to me that port manipulation is useful for controlling the pins on my arduino, I'm attempting to control pins on two 74hc595 shift registers attached to my arduino.

    am I missing something here?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    Yup, you nailed it, good advice but for the wrong IO, sorry about that, looks like BOB has it nailed though so now you have two ways depending on your hardware setup

     

    Good luck

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube