Working on first project (homework) and could use some guidance

Going through the typical weekend homework and one item on the list is three programs on Arduino. The tricky part is that we haven’t gone over very much in class, only making the sample projects. A picture of the problem is attached.

I recognize that a counter should be used in this case, so that is my first thing to look at. Any advice or pointers past this would be greatly appreciated!

Mechanical_Mind: I recognize that a counter should be used in this case...

Bitmask would also work.

Any advice or pointers past this would be greatly appreciated!

Stop fannying about and write some code. Anything is better than nothing. Once you have something, working or not, you can ask detailed questions which will very likely get you detailed answers.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Will you be actually building this circuit?
8e7275c6ceb4a74b541af3bb2409a5ee780f476c.jpg
If so, don’t.
Why? Because the LEDs require series resistors to limit current through them and the output pins of the UNO.

If you are only required to produce code then okay, but please point out to your instructor/teacher that if series resistors are not used then your UNO will not last long.

What we like to see is some code to show you have attempted to accomplish your goal.
Can you post some code that at least sets up your input buttons and outputs to LEDs.
Your idea of using a counter is the way to go, so some code where you increase and decrease a variable using the buttons would be good. If you have not been instructed that far, please let us know.

Thanks… Tom… :slight_smile:

TomGeorge: If so, [u]don't[/u].

Well spotted!

We can help out when you run into problems. You should make an effort to to this, hint 2, use change in state detection with the switches.

https://www.arduino.cc/en/Tutorial/StateChangeDetection

.

fannying about

:)

Here's what I've come up with so far. It's my first time, be gentle.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPinUp = 13;     // the number of the pushbuttonup pin
const int buttonPinDown = 12    // the number of the pushbuttondown pin
const int ledPin1 =  2;      // the number of the LED pin1
const int ledPin2 =  3;      // the number of the LED pin2
const int ledPin3 =  4;      // the number of the LED pin3
const int ledPin4 =  5;      // the number of the LED pin4
const int ledPin5 =  6;      // the number of the LED pin5
const int ledPin6 =  7;      // the number of the LED pin6


int presses = 0;   // variable to store number of presses
const byte numPins = 6;   //how many leds


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinUp, INPUT);
  inMode(buttonPinDown, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  //buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (digitalRead(buttonPinUp)==HIGH) 
  {
    count++;
  }
  if (digitalRead(buttonPinDown)==HIGH)
  {
    count--;
  }
    
}

Thanks!

Cannot see your images.

You need to understand "changed state detection" as linked to.

.

Hint. The loop function runs at thousands of time a second so when you hold you button down the count variable will almost instantly get a stupidly large number in it. You physically can not hold the button down for that short a time to give you only a count of one.

That is why you need a state change, or a while loop that holds the code until you release the button following your detection of it being pressed.

This is all without any consideration of "contact bounce" which is a phrase well worth googling.

Mechanical_Mind: Here's what I've come up with so far.

Good start.

Display count using Serial.println. LarryD's and Grumpy_Mike's posts should then make more sense.

Progress! Can’t get the “volume” to go down yet.

// set pin numbers:
const int up = 13;     // Volume-up pushbutton pin
const int down = 12;  // Volume-down pushbutton pin
const int LED1 = 2;   //First LED, min volume indication
const int LED2 = 3;   //Second LED
const int LED3 = 4;   //Third LED
const int LED4 = 5;   //Fourth LED
const int LED5 = 6;   //Fifth LED
const int LED6 = 7;  //Top LED, max volume indication

// variables will change:
int upstate = 0;     //varioable for reading up-button status
int downstate = 0;   //variable for reading down-butone status
int volume = 0;      //variable to track current volume state
int buttonstate = LOW;

void setup() {
  // initialize the LED pin as an output:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  // initialize the pushbutton pin as an inputs:
  pinMode(up, INPUT);
  pinMode(down, INPUT);
}

void loop() {
  // read the state of the pushbuttons:
  upstate = digitalRead(up);
  downstate = digitalRead(down);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (upstate == HIGH && volume < 6) {  //if volume is not max level
    digitalWrite(LED1 + volume, HIGH);  //turn more LEDs on
    volume = volume+1;  //increas the volume by one
  }

  //the downbutton State is HIGH:
  if (downstate == HIGH && volume > 0)  {   //if volume is not min level
   // turn LED on:
   volume = volume-1;  //decrease the volume by 1
    digitalWrite(LED1 == volume, HIGH);  //turn more LEDs off
     
  } 
delay(200);  //some delay to stabalize pushbuttones
}

Thanks for admitting up-front that it's homework.

I'll just unpack that requirement a bit:

"When the state of the top button [u]changes from LOW to HIGH[/u], then [u]if all leds are not already turned on[/u], turn on the one led that needs to be tuned on to make the lit portion of the bar increase in size by one. "

Of course, you'll need a variable "number of leds currently turned on" and variables for the prior stet of the button - I'm sure the button example shows this. Because the LEDs are connected to pins 5-10 in order, working out which LED needs to go on/off when a button is pressed is just a matter of subtracting.

digitalWrite(LED1 == volume, HIGH);  //turn more LEDs off

This makes no sense at all. Using a comparison equals in a write statement is just silly.

Why are you ignoring replies #7&8? Use a print statement to see the value in your volume variable.