code issues.


i'm new programming, i'm trying write code uses button select program run. issue i'm having when delay value high program times not register button change.  it can hit or miss. thankful help. here code, sorry it's messy.



code: [select]
// constants won't change. they're used here set pin numbers:
int buttonpin1 = 7;     // number of pushbutton pin
int buttonpin2 = 6;
int counter = 0; //counter 1
int analogpin1 = 0; // select input pin potentiometer
int pin13 = 13;    // select pin led
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int sensorvalue;  // variable store value coming sensor
int mappedval;
// variables change:
int buttonstate1;        // variable reading pushbutton status
int buttonstate2;
int lastbuttonstate1;
int val9 = 0;       // variable holds current led brightness
int delayval = 0;  // variable holds current delay time
int val10 = 0;       // variable holds current led brightness
int delayval2 = 0;  // variable holds current delay time
int val11 = 0;       // variable holds current led brightness
int delayval3 = 0;  // variable holds current delay time

void setup() {
 // initialize led pin output:
 pinmode(pin13, output);      
 // initialize pushbutton pin input:
 pinmode(buttonpin1, input);  
 pinmode(buttonpin2, input);  
 pinmode(pin11, output);
 pinmode(pin10, output);
 pinmode(pin9, output);
serial.begin(115200);
}


void loop(){
 // read state of pushbutton value:

 buttonstate2 = digitalread(buttonpin2);
 buttonstate1 = digitalread(buttonpin1);
 // compare buttonstate previous state
 if (buttonstate1 != lastbuttonstate1) {
   // if state has changed, increment counter
   if (buttonstate1 == high) {
     // if current state high button
     // wend off on:
     counter++;
      }
     }
 // save current state last state,
 //for next time through loop
 lastbuttonstate1 = buttonstate1;
   // reset count if on max mode number
   if(counter > 8)
   {
     counter = 0;
   }




 
 if (counter == 1) {    
         // read value sensor:
          sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
         // turn ledpin on
          digitalwrite(pin13, high);
          digitalwrite(pin11, high);
          digitalwrite(pin10, low);
          digitalwrite(pin9, low);
          // stop program <sensorvalue> milliseconds:
           
         sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
          delay(mappedval);
          // turn ledpin off:        
          digitalwrite(pin13, high);
          digitalwrite(pin11, low);
          digitalwrite(pin10, high);
          digitalwrite(pin9, low);
         sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
          delay(mappedval);
          digitalwrite(pin13, high);
          digitalwrite(pin11, low);
          digitalwrite(pin10, low);
          digitalwrite(pin9, high);
          sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
          delay(mappedval);
          digitalwrite(pin9, low);
//           serial.print("\t");  
//           serial.println(mappedval);      
         

 }
 
 
 
  buttonstate1 = digitalread(buttonpin1);
 // compare buttonstate previous state
 if (buttonstate1 != lastbuttonstate1) {
   // if state has changed, increment counter
   if (buttonstate1 == high) {
     // if current state high button
     // wend off on:
     counter++;
      }
     }
 // save current state last state,
 //for next time through loop
 lastbuttonstate1 = buttonstate1;
   // reset count if on max mode number
   if(counter > 8)
   {
     counter = 0;
   }


 else if(counter == 2) {
          sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
         // turn ledpin on
          digitalwrite(pin13, high);
          digitalwrite(pin11, low);
          digitalwrite(pin10, high);
          digitalwrite(pin9, low);
          // stop program <sensorvalue> milliseconds:
           
       //  sensorvalue = analogread(analogpin1);
       //   mappedval = map(sensorvalue, 0, 1023, 5, 300);
       //   delay(mappedval);
          // turn ledpin off:        
        //  digitalwrite(pin13, high);
         // digitalwrite(pin11, low);
        //  digitalwrite(pin10, high);
       //   digitalwrite(pin9, low);
         sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
          delay(mappedval);
          digitalwrite(pin13, high);
          digitalwrite(pin11, low);
          digitalwrite(pin10, low);
          digitalwrite(pin9, high);
          sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 5, 300);
          delay(mappedval);
          digitalwrite(pin9, low);
 }
 
 
 
  buttonstate1 = digitalread(buttonpin1);
 // compare buttonstate previous state
 if (buttonstate1 != lastbuttonstate1) {
   // if state has changed, increment counter
   if (buttonstate1 == high) {
     // if current state high button
     // wend off on:
     counter++;
      }
     }
 // save current state last state,
 //for next time through loop
 lastbuttonstate1 = buttonstate1;
   // reset count if on max mode number
   if(counter > 8)
   {
     counter = 0;
   }

   
         else if(counter == 3){
         sensorvalue = analogread(analogpin1);
          mappedval = map(sensorvalue, 0, 1023, 1, 255);  
           
         val9 = random(1,mappedval);       // pick random number between 100 , 255
         analogwrite(pin9, val9);    // set led brightness
         val10 = random(1,mappedval);       // pick random number between 100 , 255
         analogwrite(pin10, val10);    // set led brightness
         val11 = random(1,mappedval);       // pick random number between 100 , 255
         analogwrite(pin11, val11);    // set led brightness
           // set led brightness
         delayval = random(50,150);   // pick random number between 30 , 100
         delay(delayval);             // delay many milliseconds
         }



 buttonstate1 = digitalread(buttonpin1);
 // compare buttonstate previous state
 if (buttonstate1 != lastbuttonstate1) {
   // if state has changed, increment counter
   if (buttonstate1 == high) {
     // if current state high button
     // wend off on:
     counter++;
      }
     }
 // save current state last state,
 //for next time through loop
 lastbuttonstate1 = buttonstate1;
   // reset count if on max mode number
   if(counter > 8)
   {
     counter = 0;
   }
       
         
         
  else if(counter == 4){
    sensorvalue = analogread(analogpin1);
    mappedval = map(sensorvalue, 0, 1023, 5, 300);
    digitalwrite(9, high);
    digitalwrite(10, high);
    digitalwrite(11, high);
   delay(mappedval);
   digitalwrite(9, low);
   digitalwrite(10, low);
   digitalwrite(11, low);
  }  
 
 
 
  buttonstate1 = digitalread(buttonpin1);
 // compare buttonstate previous state
 if (buttonstate1 != lastbuttonstate1) {
   // if state has changed, increment counter
   if (buttonstate1 == high) {
     // if current state high button
     // wend off on:
     counter++;
      }
     }
 // save current state last state,
 //for next time through loop
 lastbuttonstate1 = buttonstate1;
   // reset count if on max mode number
   if(counter > 8)
   {
     counter = 0;
   }


 else if(counter == 5){
 digitalwrite(9, high);
 delay(50);
  }
 else if(counter == 6){
 digitalwrite(10, high);
 delay(50);
 }
 else if(counter == 7){
 digitalwrite(11, high);
 delay(50);
 }
 
  else if(counter == 8 ){
  digitalwrite(9, high);
  digitalwrite(10, high);
  digitalwrite(11, high);
   delay(50);
   }
   
else
{
digitalwrite(9, low);
digitalwrite(10, low);
digitalwrite(11, low);
}
serial.println(mappedval);          
serial.println(counter);
}


:)

yeah, delay stops entire program.  look @ 'blink without delay' example under examples--> digital--> blink without delay



Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > code issues.


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums