Please some help

Hello, I am trying to write a program to response to the push button in the following way:

(1) when not pressed, you program should repeatedly print out number from 0 to 100, increase by 1 per loop. The output should go to the serial port so that it can be observed from arduino.

(2) when push bottom is pressed, the counter should be reset to 0, and the future
loop should count up to the value of the counter at the time the bottom is pressed.

Example output:
0 1 2 3 4 ...100 0 1 2 3 ... 43 (button pressed) 0 1 2 ... 43 0 1 2 ... 43 0 1 2 ... 29 (button pressed again) 0 1 2 ... 29 ....

I already wrote this program, it works for the first part on counting 0-100, but I am still working to modify the part about push button. Please give me some help.

int inPin = 7;
int val = 0;

void setup() {
pinMode(inPin, INPUT);
Serial.begin(9600);
for (int j=1; j<=100; ++j) {
Serial.println(j); // Send j in decimal format
delay(500);
}
}
void loop() { val = digitalRead(inPin); // read input value
}
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}

The setup execute only once - seems you are saying you want something that keeps going... where do you think that should be?

You mention you need to remember the value when button is presssed, how does a program 'remembers' something?

Have no clue..

What's your programing experience in general and on Arduino ?

Not sure if you are making pin 7 go high or pin 7 go low on close but you can move
val = j;
j = 1;

And you didn't declare ledpin

int inPin = 7;
int val = 100;

void setup() {
  pinMode(inPin, INPUT);
  Serial.begin(9600);
}
void loop() { 
  for (int j=1; j<=val; ++j) {
    Serial.println(j); // Send j in decimal format
    val = digitalRead(inPin);  // read input value
    if (val == HIGH) {         // check if the input is HIGH (button released)
      val = j;
      j = 1;
      digitalWrite(ledPin, LOW);  // turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);  // turn LED ON
    }
    delay(500);
  }
}

Why     val = digitalRead(inPin);  // read input value?

messing up with Val that has nothing to do with reading a pin status does not seem a best design practice ??

Beginner. I just start writing my first program. I am doing kind of not bad on it. The counter part took couple of minutes, but I just stuck in the push button part.

Actually I missed the val = digital read

should be

int inPin = 7;
int val = 100;
int pinval;

void setup() {
  pinMode(inPin, INPUT);
  Serial.begin(9600);
}
void loop() { 
  for (int j=1; j<=val; ++j) {
    Serial.println(j); // Send j in decimal format

    pinval = digitalRead(inPin);  // read input value

    if (pinval == HIGH) {         // check if the input is HIGH (button released)
      val = j;
      j = 1;
      digitalWrite(ledPin, LOW);  // turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);  // turn LED ON
    }
    delay(500);
  }
}

Ever heard about bouncing?

You need to begin by declaring all your variables at the top of the program.

I would suggest this means you need

int ledPin = 7;
int inPin = 0;
int buttonState = 0;
int buttonState1 = 0;
int prevVal = 200;
int debounceCount;
int counter;

in your setup (which runs only once) you need to declare your inputs and outputs and start your serial communications.

Serial.begin(9600);
pinMode(inPin, INPUT):
pinMode(ledPin, OUTPUT);

in your main loop i would suggest you put something like

if((millis() % 10) == 0)
{
MAIN CODE HERE
}

this provides you a 10ms delay between reads without pausing the program. I believe this to be good practice for the future as using delays can be problematic if you want to run things in parallel.

this would mean you now use an if statement and counter for your button read.

i would do this as follows:

buttonState1 = digitalRead(inPin);

if(buttonState1 == 1)
{
 debounceCount++;
if(debounceCount > 49) // 500ms
{
debounceCount = 0;
buttonState = 1;
}
}
else if(debounceCount != 0)
{
debounceCount--;
}

if(buttonState == 1)
{
prevVal = counter;
counter = 0;
buttonState = 0;
}

if(((millis % 500) == 0) && (counter < prevVal))
{
counter++;
}

not sure what your plan is when this value is reached or for the LED.
hope this helps, try to assemble the blocks.

For this assignment, which appears to be school work, a properly wired switch is required. I do not think that any debouncing is needed for this particular specification.

Properly wired or not deboucing IS needed. it can be done in hardware or software

jmanatee:
Actually I missed the val = digital read

should be

int inPin = 7;

int val = 100;
int pinval;

void setup() {
  pinMode(inPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  for (int j=1; j<=val; ++j) {
    Serial.println(j); // Send j in decimal format

pinval = digitalRead(inPin);  // read input value

if (pinval == HIGH) {        // check if the input is HIGH (button released)
      val = j;
      j = 1;
      digitalWrite(ledPin, LOW);  // turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);  // turn LED ON
    }
    delay(500);
  }
}

I am getting " exit status 1 'pinval' was not declared in this scope " when I do upload for the code.

uobstudent:
You need to begin by declaring all your variables at the top of the program.

I would suggest this means you need

int ledPin = 7;

int inPin = 0;
int buttonState = 0;
int buttonState1 = 0;
int prevVal = 200;
int debounceCount;
int counter;




in your setup (which runs only once) you need to declare your inputs and outputs and start your serial communications.



Serial.begin(9600);
pinMode(inPin, INPUT):
pinMode(ledPin, OUTPUT);




in your main loop i would suggest you put something like 

if((millis() % 10) == 0)
{
MAIN CODE HERE
}

this provides you a 10ms delay between reads without pausing the program. I believe this to be good practice for the future as using delays can be problematic if you want to run things in parallel.

this would mean you now use an if statement and counter for your button read.

i would do this as follows:



buttonState1 = digitalRead(inPin);

if(buttonState1 == 1)
{
debounceCount++;
if(debounceCount > 49) // 500ms
{
debounceCount = 0;
buttonState = 1;
}
}
else if(debounceCount != 0)
{
debounceCount--;
}

if(buttonState == 1)
{
prevVal = counter;
counter = 0;
buttonState = 0;
}

if(((millis % 500) == 0) && (counter < prevVal))
{
counter++;
}




not sure what your plan is when this value is reached or for the LED.
hope this helps, try to assemble the blocks.

I am getting the same message 'exit status 1 'pinval' was not declared in this scope ' for this code as well!!.

I am getting the same message 'exit status 1 'pinval' was not declared in this scope ' for this code as well!!.

Post the COMPLETE code, in code tags, and the EXACT error messages - including line numbers.

int ledPin = 7;
int inPin = 0;
int val = 100;
int buttonState = 0;
int buttonState1 = 0;
int prevVal = 200;
int debounceCount;
int counter;

void setup() {
  pinMode(inPin, INPUT);
  Serial.begin(9600);
}
void loop() { 
  for (int j=1; j<=val; ++j) {
    Serial.println(j); // Send j in decimal format

    pinval = digitalRead(inPin);  // read input value

    if (pinval == HIGH) {         // check if the input is HIGH (button released)
      val = j;
      j = 1;
      digitalWrite(ledPin, LOW);  // turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);  // turn LED ON
    }
    delay(500);
  }
}

I will get: "exit status 1 'pinval' was not declared in this scope"

Well, what scope do you think it is declared in?

Make this:

    pinval = digitalRead(inPin);  // read input value

this:

    int pinval = digitalRead(inPin);  // read input value

This program is just printing a list of 1s

Vsumitsu:
This program is just printing a list of 1s

I don't see a program!

This program is just printing a list of 1s

What are you expecting it to print? How is your switch wired? It appears as though the pin that the switch is connected to is always HIGH, so the value of j is continually reset to 1.