hello, i'm using crowduino nano 328 for my project.
here's my code
const int cmd = 5; // pin command for printing
const int coin = 0; // pin for 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 for coin
unsigned long printTimer; // temporary time handler for printing
unsigned long pulseTimer; // temporary time handler for cmd pulse
char cArray[9]; // inline coin count for printing
int idx = 0; // pointer for coin array
int idxS = 0; // command pointer
void setup(){ //This is where setup should be done.
pinMode(cmd,OUTPUT); // assigning pin as 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 for onother inputs.
cArray[idx] = cCount; //save count to array.
if(idx==8){idx=0;}else{idx++;} //check if pointer,increment if less than the size of array otherwise reset.
cCount = 0; //reset coin counter
ind = false; //reset coin indicator
}
}
void detectCoin(){ //this is the ISR(Interrupt Sub Routine
cCount++; //increment counter for every pulse
ind = true; //enable indicator
insertTimer = millis(); //get time stamp
}
void sendCommand(){ //This is where Command is send to host(Thread style execution)
if(cArray[idxS]>0 && printTimer==0){printTimer = millis();} //is specific array has a value to send? then set printerTimer.
else if(cArray[idxS]>0 && printTimer>0){ //is timer set and array at location idxS has value?
if(!sndNow){ //should i send high pulse?
pulseTimer = millis(); //get time stamp for pulse timing
digitalWrite(cmd,HIGH); //send high pulse
sndNow = true; //indicate that i already sent high pulse
}else{
if(millis()-pulseTimer==500){ //ofcourse high pulse is sent and were now checking the time for a period of 0.5sec
digitalWrite(cmd,LOW); //0.5 sec is reached, then pull down the sending line
}else if(millis()-pulseTimer>=1000){ //wait for 0.5sec again after pulling down the line
sndNow = false; //0.5sec pulldown is reached,we now reset indicators and timers for the next pulse if any.
pulseTimer = 0;
cArray[idxS]--; //ofcourse decrement the value after sending every pulse.
}
}
}else if(printTimer>0){ //all pulse is sent and were now waiting for printer to finish his job.
if(millis()-printTimer>=8000){ //printer is done, we can now proceed to next index of array.
printTimer = 0;
}
}else if(cArray[idxS]==0){ //array has no data to send?
if(idxS==8){idxS=0;} //well check its index if less than the size of the array.
else {idxS++;} //increment if less otherwise reset to zero.
}
}
it was okay on my 1st upload but now it keeps on firing pulses without any input. i tried changing power source from usb or directed vin input but no luck.
This code seems to work fine but when I hold the input pin"wire" the pulses go crazy.
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 help XD