Coin counter to Interrupt pin. help.


hello, i'm using crowduino  nano 328 project.

here's code

code: [select]


const int cmd = 5;         // pin command printing
const int coin = 0;        // pin coinsense
char ccount = 0;           // temporary coin value handler
boolean ind = false;       // coin indicator
boolean pulse = false;     // ongoing pulse indicator
boolean sndnow = false;    // high pulse sending indicator
unsigned long inserttimer; // temporary time handler coin
unsigned long printtimer;  // temporary time handler printing
unsigned long pulsetimer;  // temporary time handler cmd pulse
char carray[9];            // inline coin count printing
int idx = 0;               // pointer coin array
int idxs = 0;              // command pointer


void setup(){              //this setup should done.
  pinmode(cmd,output);     // assigning pin output
  digitalwrite(cmd,low);   // pull down command first
  attachinterrupt(coin, detectcoin, rising); // start coin interrupt 
}

void loop(){             
  getcoincount();
  sendcommand();
}


void getcoincount(){
  if(ind && (millis()-inserttimer >= 3000)){  //3 sec period of consecutive input coins.
    inserttimer = 0;                          //reset timer onother inputs.
    carray[idx] = ccount;                     //save count array.
    if(idx==8){idx=0;}else{idx++;}            //check if pointer,increment if less size of array otherwise reset.
    ccount = 0;                               //reset coin counter
    ind = false;                              //reset coin indicator
  }
}

void detectcoin(){                            //this isr(interrupt sub routine
    ccount++;                                 //increment counter every pulse
    ind = true;                               //enable indicator
    inserttimer = millis();                   //get time stamp
}

void sendcommand(){                                            //this command send host(thread style execution)
  if(carray[idxs]>0 && printtimer==0){printtimer = millis();}  //is specific array has value send? set printertimer.
  else if(carray[idxs]>0 && printtimer>0){                     //is timer set , array @ location idxs has value?
       if(!sndnow){                                            //should send high pulse?
          pulsetimer = millis();                               //get time stamp pulse timing               
          digitalwrite(cmd,high);                              //send high pulse
          sndnow = true;                                       //indicate sent high pulse
        }else{
          if(millis()-pulsetimer==500){                        //ofcourse high pulse sent , checking time period of 0.5sec
            digitalwrite(cmd,low);                             //0.5 sec reached, pull down sending line
          }else if(millis()-pulsetimer>=1000){                 //wait 0.5sec again after pulling down line
           sndnow = false;                                     //0.5sec pulldown reached,we reset indicators , timers next pulse if any.
           pulsetimer = 0;
           carray[idxs]--;                                     //ofcourse decrement value after sending every pulse.
          }
        }
  }else if(printtimer>0){                                     //all pulse sent , waiting printer finish job.
        if(millis()-printtimer>=8000){                        //printer done, can proceed next index of array.
          printtimer = 0;
        }
  }else if(carray[idxs]==0){                                  //array has no data send?
        if(idxs==8){idxs=0;}                                  //well check index if less size of array.
        else {idxs++;}                                        //increment if less otherwise reset zero.
  }
}



it okay on 1st upload keeps on firing pulses without input. tried changing power source usb or directed vin input no luck.


this code seems work fine when hold input pin"wire" pulses go crazy.

code: [select]
int cmd = 8;
int coin = 2;

void setup() {

pinmode(coin, input);
pinmode(cmd, output);
digitalwrite(cmd, low);

}

void loop() {
  if(digitalread(coin)==high)
  {digitalwrite(cmd, high);
  }
  else{
    digitalwrite(cmd, low);
  }

}


anyone xd

for interrupt pin, have set expect signal (i.e. input)?

are using internal pull-up resistor or external?

code: [select]
pinmode(mypin, input_pullup);





Arduino Forum > Using Arduino > Programming Questions > Coin counter to Interrupt pin. help.


arduino

Comments