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 8742881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:34:37+00:00 2026-06-13T11:34:37+00:00

In my Arduino IDE I have set up a program that runs eight LEDs

  • 0

In my Arduino IDE I have set up a program that runs eight LEDs through a shift register and now am trying to make a class to control the shift register. So far I have created the file and created a constructor with a few functions for the class, but when I try to verify the code the IDE says that I am redefining the class shiftreg here is the error message:

In file included from Lab9_step3.cpp:97:
shiftreg.h:2: error: redefinition of 'class shiftreg'
shiftreg.h:3: error: previous definition of 'class shiftreg'

and my code for lab_9 is:

 /*     ---------------------------------------------------------
 *     |  Arduino Experimentation Kit Example Code             |
 *     |  CIRC-05 .: 8 More LEDs :. (74HC595 Shift Register)   |
 *     ---------------------------------------------------------
 * 
 * We have already controlled 8 LEDs however this does it in a slightly
 * different manner. Rather than using 8 pins we will use just three
 * and an additional chip.
 *
 *
 */
#include "shiftreg.h"

//Pin Definitions
//Pin Definitions
//The 74HC595 uses a serial communication 
//link which has three pins
shiftreg a(2, 3, 4);

int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor




/*
 * setup() - this function runs once when you turn your Arduino on
 * We set the three control pins to outputs
 */
void setup()
{
  a.pinmode();
}

/*
 * loop() - this function will start after setup finishes and then repeat
 * we set which LEDs we want on then call a routine which sends the states to the 74HC595
 */
void loop()                     // run over and over again
{ 
   for(int i = 0; i < 256; i++){
   a.update(i);

    // read the value from the sensor:
    sensorValue = analogRead(sensorPin); 

   delay(sensorValue); 
  }
}



    /******************************

    /*
     * updateLEDsLong() - sends the LED states set in ledStates to the 74HC595
     * sequence. Same as updateLEDs except the shifting out is done in software
     * so you can see what is happening.
     */ 
//    void updateLEDsLong(int value){
//      digitalWrite(latch, LOW);    //Pulls the chips latch low
//      for(int i = 0; i < 8; i++){  //Will repeat 8 times (once for each bit)
//      int bit = value & B10000000; //We use a "bitmask" to select only the eighth 
//                                   //bit in our number (the one we are addressing this time thro
//                        //ugh
//      value = value << 1;          //we move our number up one bit value so next time bit 7 will
//                        // be
//                                   //bit 8 and we will do our math on it
//      if(bit == 128){digitalWrite(data, HIGH);} //if bit 8 is set then set our data pin high
//      else{digitalWrite(data, LOW);}            //if bit 8 is unset then set the data pin low
//      digitalWrite(clock, HIGH);                //the next three lines pulse the clock pin
//      delay(1);
//      digitalWrite(clock, LOW);
//      }
//      digitalWrite(latch, HIGH);  //pulls the latch high shifting our data into being displayed
//    }
//    
//    
//    //These are used in the bitwise math that we use to change individual LEDs
//    //For more details http://en.wikipedia.org/wiki/Bitwise_operation
//    int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
//    int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
//    /*
//     * changeLED(int led, int state) - changes an individual LED 
//     * LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON
//     */
//     void changeLED(int led, int state){
//       ledState = ledState & masks[led];  //clears ledState of the bit we are addressing
//       if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to le
//                        //dState
//       updateLEDs(ledState);              //send the new LED state to the shift register
//     }
//     **********************************/

and my code for shiftreg.h is:

/*Shift Register
*/               (Error occurs here)
class shiftreg   (and here)
{
  private:
    //Pin Definitions
    //Pin Definitions
    //The 74HC595 uses a serial communication 
    //link which has three pins
    int data; 
    int clock;
    int latch;

  public:
  shiftreg (int _data, int _clock, int _latch);
  void update(int value);
  void pinmode();
};

and my code for shiftreg.cpp is:

#include "shiftreg.h"

/*shiftreg constructor:
*/
shiftreg::shiftreg (int _data, int _clock, int _latch)
{
    data = _data; 
    clock = _clock;
    latch = _latch;

    //Used for single LED manipulation
    int ledState = 0;
    const int ON = HIGH;
    const int OFF = LOW;
}

/*
 * updateLEDs() - sends the LED states set in ledStates to the 74HC595
 * sequence
 */
void shiftreg::update(int value)
{
  digitalWrite(latch, LOW);     //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
}

/*
*/
void shiftreg::pinmode()
{
    pinMode(data, OUTPUT);
    pinMode(clock, OUTPUT);
    pinMode(latch, OUTPUT);
}

Thanks for the help!

  • 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-06-13T11:34:38+00:00Added an answer on June 13, 2026 at 11:34 am

    According to the error message you’re including shiftreg.h on line 97 of Lab9_step3.cpp. What’s on the 96 lines above that? The error probably lies somewhere in there.

    If I had to guess, I’d say you’re, either directly, or indirectly, including shiftreg.h twice (or more) in Lab9_step3.cpp, and the error’s occurring because you don’t have include guards in your header file.

    Try adding the following to shiftreg.h

    #ifndef SHIFTREG_H
    #define SHIFTREG_H
    
    class shiftreg
    {
      // ...
    };
    
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to program my Arduino through Eclipse (because Eclipse is the easiest
I have an Arduino that is processing a string by splitting it into an
I have the following code on my Arduino that constantly checks for a serial
I have an Arduino-based device which connects through USB. I'd like to detect it
I have a simple Arduino sketch that spans several files. There is a function
I have a Arduino app that needs to talk to my PC across the
I have an Arduino sketch that takes a timet and when that timet is
I have an Arduino sketch that takes a timet and when that timet is
I have an arduino program where I want to store data in a dynamic
I am trying to learn programming on hardware, and have ordered an Arduino for

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.