Project Help

Hi Everyone,
I am wondering if someone could please look over my source code and possibly help me out with fixing it. I am completely stumped on what to do. Thanks in advance!

const int ledPins[] = {      // pins that LED's are attached too
  13, 12, 11, 10 };
const int buttonPin1 = 3;
const int buttonPin2 = 4;  // pins switches are attached too
const int buttonPin3 = 5;
const int mPin = 52;        // pin motor is attached too
int pinCount = 8
// Variables will change:
int buttonPushCounter = 0 // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;  



void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin1 3, INPUT);
  pinMode(buttonPin2 4, INPUT);
  pinMode(buttonPin3 5, INPUT);
  //initialize the LED as an output:
  pinMode(ledPin 13, OUTPUT);
  pinMode(ledPin 12, OUTPUT);
  pinMode(ledPin 11, OUTPUT);
  pinMode(ledPIn 10, OUTPUT);
  pinMode(mPin 52, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every  button push by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 2 == 1 ) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
    
     switch (buttonPin3) {
  case 'a':    // High Speed
     if (buttonPushCounter % 2 == 1) {
    digitalWrite(ledPin 13, HIGH);
    digitalWrite(ledPin 12, HIGH);
    digitalWrite(ledPin 11, HIGH);
    digitalWrite(mPin, HIGH);
  } else {
   digitalWrite(ledPin 13, LOW);
   digitalWrite(ledPin 12, LOW);
   digitalWrite(ledPin 11, LOW);
   digitalWrite(mPin, LOW);
   }
    break;
     default:
      // turn all LEDs off:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
}

     }
      switch (buttonPin4) {
   case 'b': // medium speed
     if (buttonPushCounter % 2 == 1); {
    digitalWrite(ledPin 12, HIGH);
    digitalWrite(ledPin 11, HIGH);
    digitalWrite(mPin, HIGH);
  }
  else {
   digitalWrite(ledPin 13, LOW);
   digitalWrite(ledPin 12, LOW);
   digitalWrite(ledPin 11, LOW);
   digitalWrite(mPin, LOW);
     
   }
      switch (buttonPin5) {
    case 'c': // low speed
       if (buttonPushCounter % 2 == 1) {
    digitalWrite(ledPin 11, HIGH);
    digitalWrite(mPin, HIGH);
  } else {
   digitalWrite(ledPin 13, LOW);
   digitalWrite(ledPin 12, LOW);
   digitalWrite(ledPin 11, LOW);
   digitalWrite(mPin, LOW);
   
  }
      }
     
     case 'd': //battery indicator

Before you repost your code, you could try using the auto-format tool (ctrl-T) in the IDE, which will help sort out your indentation.

I am wondering if someone could please look over my source code and possibly help me out with fixing it. I am completely stumped on what to do.

You know when you doctor leans forward and says "What seems to be the problem" ?
Well, you need to tell us.
Don't be embarrassed.

What they said

But also, your syntax isnt correct for things like pinMode and digitalWrite - just at a glance.

ie it should be something like this, for this one piece.

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  //initialize the LED as an output:
  pinMode(ledPin[1], OUTPUT);
  pinMode(ledPin[2], OUTPUT);
  pinMode(ledPin[3], OUTPUT);
  pinMode(ledPIn[4], OUTPUT);
  pinMode(mPin, OUTPUT);
  Serial.begin(9600);
}

Not sure why you used an array for but the led pins, and not for button pins...

First things first though - what is the problem exactly you are having? You get a bunch of errors at compile and you dont know how to understand them?

J

Thank you everyone for your responses. I really appreciate it!

KE7GKP:
Can you tell us something about your code?
What is the code supposed to do?
What is it not doing that is is supposed to be doing?
What is it doing that it is not supposed to do?
Where is the rest of the code? It seems to stop in mid-stream.

Without information like that it will be very difficult to offer any help.

Please use the code button (with the #) when you post code here.

Q 1&2:

PROJECT:
The premise of the project that I am working on is a portable rechargeable fan with 3 speeds. For instance, you click button 1 and the fan is on high speed and 3 LED lights go on. You push button 2 and the fan is on a medium speed and 2 lights go on. You push button 3 and the fan is on a low speed and only one light goes on. and if you push any of the buttons again then the fan goes off. Then a fourth LED will show the level of charge. I am trying to run this portable fan on a 9-12v rechargeable battery (if possible). I am running into problems with how to wire this project on my breadboard for these 3 momentary switches to do the actions listed above.
The second part of my project is to create an induction charger (plug into the wall) which will also act as the fan's stand. Kind of like how an electric toothbrush works.

Q3 & Q4:
Right now I have 2 LED lights set up with 2 momentary (tactile Switches) hooked up. I have been able to get the two lights to go on and off with the push of either of the switches. But that is the problem, when I click the B1 (button 1) both lights go on and that is fine. However, when I push B2 (button 2) both of the lights still go on. When I push B2 only that light and the one below it should go on. If possible, I would like the 3 LED lights to be chasing each other when the battery is charging and then stop when the battery is fully charged. I also want the chip to sleep after about 10 minutes of non use. Does that make sense?

Q5:
I ended up stopping at that point and decided to ask for help. The battery indicator (the incomplete code) is supposed to be green when it is charged, amber when 25% of battery is left and red when 15% <=.

JADB:
What they said

But also, your syntax isnt correct for things like pinMode and digitalWrite - just at a glance.

ie it should be something like this, for this one piece.

void setup() {

// initialize the button pin as a input:
 pinMode(buttonPin1, INPUT);
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 //initialize the LED as an output:
 pinMode(ledPin[1], OUTPUT);
 pinMode(ledPin[2], OUTPUT);
 pinMode(ledPin[3], OUTPUT);
 pinMode(ledPIn[4], OUTPUT);
 pinMode(mPin, OUTPUT);
 Serial.begin(9600);
}




Not sure why you used an array for but the led pins, and not for button pins...

First things first though - what is the problem exactly you are having? You get a bunch of errors at compile and you dont know how to understand them?

J

Thank you so much for the correction. I gave a background of the project and answered KE7GKP's questions above. That is also another issue that I am having as well, since I am relatively new to programming, albeit I understand the concept of it, I am getting a little confused with the errors at compile as well as writing the code. I have looked at all of the examples given on the arduino examples page, and tried to mimic and modify the code to do what I want it to do. Another issue that I am having is getting the momentary switches to work. I want the switches to be pressed once and keep having electrical current running through the circuit and when the switches are pressed again the current will stop. When I am only using one switch, I can get it to work, but I am having a hard time writing the code for multiple switches.

More specifically:

int buttonPushCounter = 0 // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

Thanks again for your responses and I hope that this makes things a little clearer for you. Let me know if I can clarify anything else or help out in any way. Sorry for being not clear in my original post, I am new to the forums.

Hi again,

I have rewritten this for you, hopefully it is of some help - its a starting point.
Note, this still needs alot of work to get it to do what you want it to do, but its a start.

What this code does is explained in the header. The motor is only ON/OFF at this stage, not variable speed, however the LED's should in theory function as you want - if I have read what you wanted correctly.
I am at work, and I do not have the Arduino IDE in front of me - so hopefully this will work for you.

Question for you though - the motor you have stated is on Pin 52 - what Arduino board are you using?
If you want a variable speed output, you should use something like analogWrite() - however this only works on specific pins.
Have a look at this for more info: http://arduino.cc/en/Reference/AnalogWrite
You should change out the lines digitalWrite(motor_pin, HIGH); etc in the code below, to use analogWrite once you have sorted out the pin you will use.

Hope this is of some help to you. As I say, I cant guarantee it will work, but it should be a start.

 //*************************************************************************
// This program has 3 buttons and 4 LED's
// When button 1 is pressed, LED 1, 2, and 3 turn ON - and Turns on the Fan
// When button 2 is pressed, LED 1, and 2 turn ON - and Turns on the Fan
// When button 3 is pressed, LED 1 turns ON - and Turns on the Fan
// If the Fan is ON, pressing any of the buttons will turn the Fan OFF.
//
// More work needs to be done to make the Fan Variable Speed.
//**************************************************************************

// pins that LED's are attached too
#define led1_pin 13
#define led2_pin 12
#define led3_pin 11
#define led4_pin 10

// pins switches are attached too
#define button1_pin 3
#define button2_pin 4
#define button3_pin 5

// pin motor is attached too
#define motor_pin 52

// Button 1 variables
int button1_PushCounter = 0;    // counter for the number of button1 presses

// Button 2 variables
int button2_PushCounter = 0;    // counter for the number of button2 presses

// Button 3 variables
int button3_PushCounter = 0;    // counter for the number of button3 presses

// Motor variables
int motor_State = 0;		// state the motor is currently in, ON/OFF

void setup() 
{
  // initialize the button pins as inputs:
  pinMode(button1_pin, INPUT);
  pinMode(button2_pin, INPUT);
  pinMode(button3_pin, INPUT);
  
  // initialize the LED pins as outputs:
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);
  pinMode(led3_pin, OUTPUT);
  pinMode(led4_pin, OUTPUT);
  
  // initialize the motor pin as an output:
  pinMode(motor_pin, OUTPUT);
  
  // setup serial port baud rate
  Serial.begin(9600);  
}


void loop() 
{
  //***************BUTTON1******************************
  // read the pushbutton input pin:
  if (digitalRead(button1_pin) == 1) 
  {
    // if the motor is currently off
    if (motor_State == 0) 
    {
      button1_PushCounter++;	// count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 1 pushes: ");
      Serial.println(button1_PushCounter, DEC);
      
      // turn on the 3 LED's
      digitalWrite(led1_pin, HIGH);
      digitalWrite(led2_pin, HIGH);
      digitalWrite(led3_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;	//motor is now on
    } 
    // if the motor is currently on
    else 
    {
      button1_PushCounter++;	// count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 1 pushes: ");
      Serial.println(button1_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;	//motor is now off
    }
  }
  
  //***************BUTTON2******************************
  // read the pushbutton input pin:
  if (digitalRead(button2_pin) == 1) 
  {
    // if the motor is currently off
    if (motor_State == 0) 
    {
      button2_PushCounter++;	// count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 2 pushes: ");
      Serial.println(button2_PushCounter, DEC);
      
      // turn on 2 LED's
      digitalWrite(led1_pin, HIGH);
      digitalWrite(led2_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;	//motor is now on
    } 
    // if the motor is currently on
    else 
    {
      button2_PushCounter++;	// count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 2 pushes: ");
      Serial.println(button2_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;	//motor is now off
    }
  }
  
  //***************BUTTON3******************************
  // read the pushbutton input pin:
  if (digitalRead(button3_pin) == 1) 
  {
    // if the motor is currently off
    if (motor_State == 0) 
    {
      button3_PushCounter++;	// count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 3 pushes: ");
      Serial.println(button3_PushCounter, DEC);
      
      // turn on 1 LED's
      digitalWrite(led1_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;	//motor is now on
    } 
    // if the motor is currently on
    else 
    {
      button3_PushCounter++;	// count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 3 pushes: ");
      Serial.println(button3_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;	//motor is now off
    }
  }
}

Hi again
Slightly improved so if a button is held down, it doesnt run the code multiple times and therefore toggle the fan on and off constantly.

Not the most efficiently written, however I set it out like this so it was fairly simple to follow with nothing really complex in there.

//*************************************************************************
// This program has 3 buttons and 4 LED's
// When button 1 is pressed, LED 1, 2, and 3 turn ON - and Turns on the Fan
// When button 2 is pressed, LED 1, and 2 turn ON - and Turns on the Fan
// When button 3 is pressed, LED 1 turns ON - and Turns on the Fan
// If the Fan is ON, pressing any of the buttons will turn the Fan OFF.
//
// More work needs to be done to make the Fan Variable Speed.
//**************************************************************************

// pins that LED's are attached too
#define led1_pin 13
#define led2_pin 12
#define led3_pin 11
#define led4_pin 10

// pins switches are attached too
#define button1_pin 3
#define button2_pin 4
#define button3_pin 5

// pin motor is attached too
#define motor_pin 52

// Button 1 variables
int button1_PushCounter = 0;    // counter for the number of button1 presses
int button1_State = 0;          // state of the button to prevent code being run multiple times if button is held down

// Button 2 variables
int button2_PushCounter = 0;    // counter for the number of button2 presses
int button2_State = 0;          // state of the button to prevent code being run multiple times if button is held down

// Button 3 variables
int button3_PushCounter = 0;    // counter for the number of button3 presses
int button3_State = 0;          // state of the button to prevent code being run multiple times if button is held down

// Motor variables
int motor_State = 0;        // state the motor is currently in, ON/OFF

void setup() 
{
  // initialize the button pins as inputs:
  pinMode(button1_pin, INPUT);
  pinMode(button2_pin, INPUT);
  pinMode(button3_pin, INPUT);
  
  // initialize the LED pins as outputs:
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);
  pinMode(led3_pin, OUTPUT);
  pinMode(led4_pin, OUTPUT);
  
  // initialize the motor pin as an output:
  pinMode(motor_pin, OUTPUT);
  
  // setup serial port baud rate
  Serial.begin(9600);  
}


void loop() 
{
  //***************BUTTON1******************************
  // read the pushbutton input pin:
  if (digitalRead(button1_pin) == 1) 
  {
    // if the motor is currently off, and button code not yet run (dont execute multiple times if button held)
    if (motor_State == 0 && button1_State == 0) 
    {
      button1_PushCounter++;    // count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 1 pushes: ");
      Serial.println(button1_PushCounter, DEC);
      
      // turn on the 3 LED's
      digitalWrite(led1_pin, HIGH);
      digitalWrite(led2_pin, HIGH);
      digitalWrite(led3_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;    //motor is now on
    } 
    // if the motor is currently on
    else if (motor_State == 1 && button1_State == 0)  
    {
      button1_PushCounter++;    // count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 1 pushes: ");
      Serial.println(button1_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;    //motor is now off
    }
    button1_State = 1; // Set button state to 1, so code isnt re-run until button is pressed again.
  }
  // Button released
  else
  {
    // Reset button state
    button1_State = 0;
  }
  
  //***************BUTTON2******************************
  // read the pushbutton input pin:
  if (digitalRead(button2_pin) == 1) 
  {
    // if the motor is currently off, and button code not yet run (dont execute multiple times if button held)
    if (motor_State == 0 && button2_State == 0) 
    {
      button2_PushCounter++;    // count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 2 pushes: ");
      Serial.println(button2_PushCounter, DEC);
      
      // turn on 2 LED's
      digitalWrite(led1_pin, HIGH);
      digitalWrite(led2_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;    //motor is now on
    } 
    // if the motor is currently on
    else if (motor_State == 1 && button2_State == 0) 
    {
      button2_PushCounter++;    // count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 2 pushes: ");
      Serial.println(button2_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;    //motor is now off
    }
    button2_State = 1; // Set button state to 1, so code isnt re-run until button is pressed again.
  }
  // Button released
  else
  {
    // Reset button state
    button2_State = 0;
  }
  
  //***************BUTTON3******************************
  // read the pushbutton input pin:
  if (digitalRead(button3_pin) == 1) 
  {
    // if the motor is currently off, and button code not yet run (dont execute multiple times if button held)
    if (motor_State == 0 && button3_State == 0) 
    {
      button3_PushCounter++;    // count the button push
      Serial.println("ON. ");   // display info to serial port
      Serial.print("Number of button 3 pushes: ");
      Serial.println(button3_PushCounter, DEC);
      
      // turn on 1 LED's
      digitalWrite(led1_pin, HIGH);
      // turn on the fan (ONLY ON/OFF CONTROL, NOT VARIABLE SPEED YET)
      digitalWrite(motor_pin, HIGH);
      // change the state of the motor
      motor_State = 1;    //motor is now on
    } 
    // if the motor is currently on
    else if (motor_State == 1 && button3_State == 0) 
    {
      button3_PushCounter++;    // count the button push
      Serial.println("OFF. ");  // display info to serial port
      Serial.print("Number of button 3 pushes: ");
      Serial.println(button3_PushCounter, DEC); 
      
      // turn off all the LED's
      digitalWrite(led1_pin, LOW);
      digitalWrite(led2_pin, LOW);
      digitalWrite(led3_pin, LOW);
      // turn off the fan 
      digitalWrite(motor_pin, LOW);
      // change the state of the motor
      motor_State = 0;    //motor is now off
    }
    button3_State = 1; // Set button state to 1, so code isnt re-run until button is pressed again.
  }
  // Button released
  else
  {
    // Reset button state
    button3_State = 0;
  }
}