Variable troubles

Hi guys, enjoying my new arduino and brushing up on my programming. I'm basically just trying to incremental a variable to control the switch case loop structure.

I can't for the life of me to get buttonpresses == 0; ::slight_smile:

also, when the fading function is running, the speed of my serialprint and buttonvalue functions slow down, is there a way around that?

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int fadeval = 0;
int buttonpresses = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
Serial.begin( 9600 ); // set the serial communication rate
}

void loop(){

serialprint();
buttonvalue();

switch (buttonpresses) {
case 1:
if (buttonpresses == 1){
fading();
}

case 2:
if (buttonpresses == 2) {
blinking();
}
case 3:
if (buttonpresses >= 3) {
buttonpresses == 0;
}
}
}

// to see what the buttonpress variable is
void serialprint() {
Serial.print(buttonpresses); // print the value
Serial.println(); // print a linefeed character
}

// checks and increments buttonpresses variable
void buttonvalue(){

val = digitalRead(inputPin); // read input value
if (val == LOW) {
buttonpresses++;
}
delay(25);
}

//------Blinking mode
void blinking(){
digitalWrite(ledPin, LOW); // turn LED OFF
delay(50);
digitalWrite(ledPin, HIGH); // turn LED ON
delay(50);
}

//------Fading mode
void fading() {

for(fadeval = 0 ; fadeval <= 255; fadeval+=5) { // fade in (from min to max)
analogWrite(ledPin, fadeval); // sets the value (range from 0-255)
delay(10);
}

for(fadeval = 255; fadeval >=0; fadeval-=5) { // fade out (from max to min)
analogWrite(ledPin, fadeval);
delay(10);
}
}

(buttonpresses == 0) means "does buttonpresses have the value of 0 currently, yes or no?"

buttonpresses = 0; means "put the value of zero into the buttonpresses variable."

Also, in your switch/case 3: buttonpresses will never be MORE than three at that location, because you only get there if it's EXACTLY three. Well, that's not entirely true. You need a break; after each case clause, or it will try to do them all. That's why you felt you needed the extra if checks, I bet.

switch (buttonpresses)
{
case 1:
  fading();
  break;
case 2:
  blinking();
  break;
case 3:
default:
  buttonpresses = 0;
  break;
}

switch (buttonpresses) {
case 1:
if (buttonpresses == 1){
fading();
}

Why the extra if check ?

if buttonpresses == 1 , you will catch it was the case 1:

just use :

switch (buttonpresses) {
case 1:
fading();
break;
case 2:
blinking();
break;
case 3:
buttonpresses = 0;
break;
}

Thanks a lot guys!

Trying to think of the best way around the buttonvalue and serial delay... basically, when it's running the fading function, it will only register a button push between the rising and falling for loops.

EJ

Don`t use delay use a milis() loop

eg time = millis() + 100;
loop
{
if ( millis() > time )
{
do work
}
}

that way there is no time locked up in loops and can read i/o etc.

Having trouble getting the millis to work, here's my code (obviously not set up right):

button still only works when starting the blink or fade etc.

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int fadeval = 0; //

int buttonPresses = 0;

int potPin = 0;
int potPin2 = 1;

int potVal = 0;

int i = 0; // for for loop incrementer
int fadeAdjust = 0; // fade value

unsigned long time = 0;

void setup() {

pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
pinMode(potPin, INPUT); // declare potpin input
pinMode(potPin2, INPUT); // declare potpin2 input
Serial.begin( 19200 ); // set the serial communication rate
}

void loop(){

time = millis()+100;

if (millis()<time){

serialprint();
buttonvalue();
}

switch (buttonPresses)
{
case 1:
blinking();
break;
case 2:
fading();
break;
case 3:
default:
buttonPresses = 0;
break;
}
}

// to see what the buttonpress variable is
void serialprint() {
Serial.print(potVal); // print the value
Serial.println(); // print a linefeed character
delay(50);
}

// checks and increments buttonpresses variable
void buttonvalue(){

val = digitalRead(inputPin); // read input value
if (val == LOW) {
buttonPresses++;
}
delay(50);
}

//------Blinking mode
void blinking(){

potVal = analogRead(potPin); // read the value from the sensor
potVal = potVal*2;

digitalWrite(ledPin, LOW); // turn LED OFF
delay(potVal);
digitalWrite(ledPin, HIGH); // turn LED ON
delay(potVal);
}

//------Fading mode
void fading() {

fadeAdjust = analogRead(potPin2); // read the value from the sensor
fadeAdjust = fadeAdjust/8;
i=fadeAdjust+1;

for(fadeval = 0 ; fadeval < 255; fadeval =- i) { // fade in (from min to max)
analogWrite(ledPin, fadeval); // sets the value (range from 0-255)

delay(10);

}

for(fadeval = 255; fadeval > 0; fadeval =- i) { // fade out (from max to min)
analogWrite(ledPin, fadeval);

delay(10);
}
}

You are not setting up time .

eg set time to millis + 100 out side of the loop eg in setup()
and when check if ( millis > time ) of ( if time < millis() )
inside the if you must set up time again for the next wait .

by memory:-

long time = 0;

setup()
{
time = millis() + 100;
}

loops()
{

if ( millis() > time )
{
time = millis();
---- do your work -----
}

}

eg

void loop(){

if (millis()<time){

serialprint();
buttonvalue();
time = millis()+100;
}

alrighty, thanks. i'll try later... and quit buggin you :slight_smile:

No problems , I`m a newbee to this too , the only way to learn is to play.