Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Troubleshooting / Re: Button deboncing in simon game on: January 22, 2011, 10:03:37 am
oh sorry, and thanks for that tip. I also changed debounce time to 100ms so you have to hold button 100ms to count, but still sometimes one click counts as 2.. very rare but it does :s
Code:
#include <TrueRandom.h>
#include <EEPROM.h>
int randled;
int val;
int allow=1;
int leds[50];
int buttons[50];
int button1=2;
int button2=3;
int button3=4;
int button4=5;
int turn=0, stepp=1;
int bt1, bt2, bt3, bt4, exbt10, exbt20, exbt30, exbt40, bt10, bt20, bt30, bt40, milis1, milis2, milis3, milis4;
int debounce=100;
int test1=0;
int test2=0;
int test3=0;
int test4=0;
unsigned long time1, time2, time3, time4;
int play=1;
int glava=1,konec=0,rec;



void setup (){
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  Serial.begin(9600);
}

void loop (){

  if(glava==1){

    if(allow==1){

      for(int i=0; i<49; i++){
        leds[i]=TrueRandom.random(8,12);
      }
      allow=0;
    }

    //RECORDING BUTTONS

    bt10=digitalRead(button1);
    if(bt10==HIGH && exbt10==LOW){
      milis1=millis();
      test1=1;
    }
    if(test1==1 && (millis()-milis1)>debounce){
      bt1=digitalRead(button1);
      if (bt1==HIGH){
        buttons[turn]=8;
        Serial.println(buttons[turn]);
        digitalWrite(8,HIGH);
        time1=millis();
        turn++;
        test1=0;
      }
    }
    if(millis()-time1>200){
      digitalWrite(8,LOW);
    }


    bt20=digitalRead(button2);
    if(bt20==HIGH && exbt20==LOW){
      milis2=millis();
      test2=1;
    }
    if(test2==1 && (millis()-milis2)>debounce){
      bt2=digitalRead(button2);
      if (bt2==HIGH){
        buttons[turn]=9;
        Serial.println(buttons[turn]);
        digitalWrite(9,HIGH);
        time2=millis();
        turn++;
        test2=0;
      }
    }
    if(millis()-time2>200){
      digitalWrite(9,LOW);
    }


    bt30=digitalRead(button3);
    if(bt30==HIGH && exbt30==LOW){
      milis3=millis();
      test3=1;
    }
    if(test3==1 && (millis()-milis3)>debounce){
      bt3=digitalRead(button3);
      if (bt3==HIGH){
        buttons[turn]=10;
        Serial.println(buttons[turn]);
        digitalWrite(10,HIGH);
        time3=millis();
        turn++;
        test3=0;
      }
    }
    if(millis()-time3>200){
      digitalWrite(10,LOW);
    }


    bt40=digitalRead(button4);
    if(bt40==HIGH && exbt40==LOW){
      milis4=millis();
      test4=1;
    }
    if(test4==1 && (millis()-milis4)>debounce){
      bt4=digitalRead(button4);
      if (bt4==HIGH){
        buttons[turn]=11;
        Serial.println(buttons[turn]);
        digitalWrite(11,HIGH);
        time4=millis();
        turn++;
        test4=0;
      }
    }
    if(millis()-time4>200){
      digitalWrite(11,LOW);
    }




    if(play==1){

      for (int k=0;k<stepp; k++){
        digitalWrite(leds[k], HIGH);
        delay(300);
        digitalWrite(leds[k], LOW);
        delay(200);
      }
      play=0;
    }


    for(int j=0; j<turn;j++){
      if (buttons[j]!=leds[j]){
        val=(stepp-1);
        rec=EEPROM.read(0);

        digitalWrite(8,HIGH);
        digitalWrite(9,HIGH);
        digitalWrite(10,HIGH);
        digitalWrite(11,HIGH);            

        if(rec>=val){
          Serial.print("Napaka, tvoj dosezek je: ");
          Serial.println(val);
          Serial.print("Rekord je: ");
          Serial.println(rec);
        }

        if(rec<val){
          Serial.print("Dosegli ste nov rekord: ");
          Serial.println(val);
          Serial.println("CESTITAM!!!");
          EEPROM.write(0, val);            
        }

        glava=0;
        konec=1;
      }
    }

    if(turn==stepp && konec!=1){
      delay(200);
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      stepp++;
      play=1;
      turn=0;
      delay(1000);
    }


  }


  exbt10=bt10;
  exbt10=bt10;
  exbt20=bt20;
  exbt30=bt30;
  exbt40=bt40;

}
2  Forum 2005-2010 (read only) / Troubleshooting / Button deboncing in simon game on: January 22, 2011, 07:54:19 am
Hi. Im preety new to programming and microcontrolers world, and i want to make simon game, so i did it without copying any code. It is working, but the problem is that i made button debouncing but looks like i made some mistake and i dont know where, Also tryed to set debounce time to 50ms, but it didnt help.
Code:
#include <TrueRandom.h>
#include <EEPROM.h>
int randled;
int val;
int allow=1;
int leds[50];
int buttons[50];
int button1=2;
int button2=3;
int button3=4;
int button4=5;
int turn=0, stepp=1;
int bt1, bt2, bt3, bt4, exbt10, exbt20, exbt30, exbt40, bt10, bt20, bt30, bt40, milis1, milis2, milis3, milis4, debounce=50, test1=0, test2=0, test3=0, test4=0;
unsigned long time1, time2, time3, time4;
int play=1;
int glava=1,konec=0,rec;



void setup (){
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
Serial.begin(9600);
}

void loop (){

if(glava==1){
  
if(allow==1){
  
  for(int i=0; i<49; i++){
    leds[i]=TrueRandom.random(8,12);
  }
allow=0;
}

//RECORDING BUTTONS

bt10=digitalRead(button1);
if(bt10==HIGH && exbt10==LOW){
milis1=millis();
test1=1;
}
if(test1==1 && (millis()-milis1)>debounce){
bt1=digitalRead(button1);
if (bt1==HIGH){
  buttons[turn]=8;
//Serial.println(buttons[turn]);
digitalWrite(8,HIGH);
time1=millis();
turn++;
test1=0;
}
}
if(millis()-time1>200){
  digitalWrite(8,LOW);
}


bt20=digitalRead(button2);
if(bt20==HIGH && exbt20==LOW){
milis2=millis();
test2=1;
}
if(test2==1 && (millis()-milis2)>debounce){
bt2=digitalRead(button2);
if (bt2==HIGH){
  buttons[turn]=9;
//Serial.println(buttons[turn]);
digitalWrite(9,HIGH);
time2=millis();
turn++;
test2=0;
}
}
if(millis()-time2>200){
  digitalWrite(9,LOW);
}


bt30=digitalRead(button3);
if(bt30==HIGH && exbt30==LOW){
milis3=millis();
test3=1;
}
if(test3==1 && (millis()-milis3)>debounce){
bt3=digitalRead(button3);
if (bt3==HIGH){
  buttons[turn]=10;
//Serial.println(buttons[turn]);
digitalWrite(10,HIGH);
time3=millis();
turn++;
test3=0;
}
}
if(millis()-time3>200){
  digitalWrite(10,LOW);
}


bt40=digitalRead(button4);
if(bt40==HIGH && exbt40==LOW){
milis4=millis();
test4=1;
}
if(test4==1 && (millis()-milis4)>debounce){
bt4=digitalRead(button4);
if (bt4==HIGH){
  buttons[turn]=11;
//Serial.println(buttons[turn]);
digitalWrite(11,HIGH);
time4=millis();
turn++;
test4=0;
}
}
if(millis()-time4>200){
  digitalWrite(11,LOW);
}




if(play==1){
  
  for (int k=0;k<stepp; k++){
    digitalWrite(leds[k], HIGH);
    delay(300);
    digitalWrite(leds[k], LOW);
    delay(200);
  }
  play=0;
}


        for(int j=0; j<turn;j++){
          if (buttons[j]!=leds[j]){
            val=(stepp-1);
            rec=EEPROM.read(0);
            
            digitalWrite(8,HIGH);
            digitalWrite(9,HIGH);
            digitalWrite(10,HIGH);
            digitalWrite(11,HIGH);            
            
            if(rec>=val){
            Serial.print("Napaka, tvoj dosezek je: ");
            Serial.println(val);
            Serial.print("Rekord je: ");
            Serial.println(rec);
            }
                    
            if(rec<val){
            Serial.print("Dosegli ste nov rekord: ");
            Serial.println(val);
            Serial.println("CESTITAM!!!");
            EEPROM.write(0, val);            
            }
            
            glava=0;
            konec=1;
          }
        }
        
        if(turn==stepp && konec!=1){
          delay(200);
          digitalWrite(8,LOW);
          digitalWrite(9,LOW);
          digitalWrite(10,LOW);
          digitalWrite(11,LOW);
          stepp++;
          play=1;
          turn=0;
          delay(1000);
        }
    

}


exbt10=bt10;
exbt10=bt10;
exbt20=bt20;
exbt30=bt30;
exbt40=bt40;

}
3  Forum 2005-2010 (read only) / Troubleshooting / Re: for loop on: September 15, 2010, 10:27:17 am
uuu ye i see that now :/
just corrected the code make a few markers when to do what, and now it works just fine...
since i'm a totaly new in programming and microcontrolers stuff, every help is more than appriciated, so guys thanks A LOT! smiley
4  Forum 2005-2010 (read only) / Troubleshooting / Re: for loop on: September 15, 2010, 03:23:46 am
am... ok when i wake up I review the code, but that was not a problem.. smiley-grin since i never set i=0; so if the serial was not available it skip one loop of while sentence, but the "i" remains the same, so the string just continue to write from position it stoped...
5  Forum 2005-2010 (read only) / Troubleshooting / Re: for loop on: September 14, 2010, 04:30:36 pm
huh i allready wrote that code for some other project(with delay, but have to use millis instead) but for now sending data with processing without reading delay works fine, and never terminate string too early... so lets assume that this isnt a problem, but i will fix it.
damn, i forgot about sending data through serial monitor... tnx for tip smiley-grin
6  Forum 2005-2010 (read only) / Troubleshooting / Re: for loop on: September 14, 2010, 04:04:04 pm
ok so the first part is the processing code to send lines of .txt

Code:
import processing.serial.*;
String lines[];
Serial myport;
int test1=1,test2=1,n=1,receive;

void setup() {
  println(Serial.list());
  lines=loadStrings("1234.txt");
  myport = new Serial (this, Serial.list()[0], 9600);
}

void draw(){
  
  if(test1==1){
  println(lines[n]);
  delay(3000);
  test1=0;  
  }  
 
  if(test2==1){
  myport.write(lines[n]);  
  test2=0;
  }
}

second part is the arduino code:

Code:
char gva[9], xva[9], yva[9], zva[9], stevilka[50];
float gval, xval, yval, zval;
int gpos=99, xpos=99, ypos=99, zpos=99, i=0, endx;

void setup() {
Serial.begin(9600);
}

void loop(){
  //RECEIVE
  while(Serial.available()){
    stevilka[i] = Serial.read();
    
  //positions (G X Y Z)
    if(stevilka[i]=='G'){
    gpos = i;
    }
    if(stevilka[i]=='X'){
    xpos = i;
    }
    if(stevilka[i]=='Y'){
    ypos = i;
    }
    if(stevilka[i]=='Z'){
    zpos = i;
    }
    
    i++;  
  }  
  stevilka[i]=0;
  
//EXAMPLE OF DECLARING A VALUE OF X IN A LINE  
endx=(ypos-xpos-1);

for (int g=0;g<endx;g++){
xva[g]=stevilka[xpos+g+1];
xva[g+1]=0;
}
xval=atof(xva);

if(endx==6){digitalWrite(13,HIGH);}

}

im sending a test line to arduino: G1X4.0435Y1.0487Z2.5430
7  Forum 2005-2010 (read only) / Troubleshooting / Re: for loop on: September 14, 2010, 01:37:30 pm
ooh yea made that correction PaulS. But the problem in short way..

Code:
ypos=9;
xpos=2;

endx=(ypos-xpos-1);

for (int g=0;g<endx;g++){
xva[g]=stevilka[xpos+g+1];
xva[g+1]=0;
}
xval=atof(xva);

if(endx==6){digitalWrite(13,HIGH);}
and the light doesnt get HIGH... so the endx is not 6, but if i remove endx from for loop, the light get HIGH... so i assume that for loop somehow change the value of endx...
unfortunately i cant use serialmonitor since i have serial input from PC, so cant check the value of endx
8  Forum 2005-2010 (read only) / Troubleshooting / for loop on: September 14, 2010, 08:22:46 am
Hi.
Im working on reading .txt line sent from PC to arduino, so arduino can read xyz values.
it is working quite good but i have one biiigggg problem :/

Code:
ypos=9;
xpos=2;

endx=(ypos-xpos-1);

for (int g=0;g<6;g++){
xva[g]=stevilka[xpos+g+1];
}
xval=atof(xva);

in this code now it works, but instead in for loop of g<6 i would like to make g<endx, but it doesnt work. I also try to make it g<(ypos-xpos-1); but with no luck. And also if i make g<endx the endx somehow is not 6 if i try.
so main question is how can i set in for loop the g<integer, because it somehow doesnt work for me. I use 0018.

ypos, xpos, endx are integers.
if you need i can post whole code
9  Forum 2005-2010 (read only) / Troubleshooting / Re: long hold mode on: July 11, 2010, 02:19:48 am
same :/
10  Forum 2005-2010 (read only) / Troubleshooting / Re: long hold mode on: July 10, 2010, 05:41:58 am
still the same :/
11  Forum 2005-2010 (read only) / Troubleshooting / Re: long hold mode on: July 09, 2010, 08:56:24 am
Here is my code. Now it works all good, but i have to use long instead of unsigned long, because i dont know why, but if i make unsigned long, the led went HIGH right after pressing the button, so there is no 2000ms waiting, and also went LOW right after next click :/
Code:
int ledpin = 2;
int buttonpin = 5;
int buttonstate = 0;
int exstate = 0;
int ledstate = 0;
long time = 0;
long time2 = 0;
int timedown = 2000;

void setup () {
pinMode(ledpin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
}

void loop () {

buttonstate = digitalRead(buttonpin);
if(buttonstate == HIGH && exstate == LOW) {
time = millis();
}

if(buttonstate == HIGH && exstate == HIGH) {
time2 = millis();
}

if((time2 - time) > timedown) {
  if(ledstate == 0){
  ledstate = 1;
  time = millis();
  time2 = millis();
  }
  
  else{
  ledstate = 0;
  time = millis();
  time2 = millis();
  }
}

if(ledstate == 0) {
digitalWrite(ledpin, LOW);
}

if(ledstate == 1) {
digitalWrite(ledpin, HIGH);
}

exstate = buttonstate;
}
12  Forum 2005-2010 (read only) / Troubleshooting / Re: long hold mode on: July 08, 2010, 01:43:20 pm
ok sorry about that post without any information, and thanks for help. with serial.println i got look into code and fix it that it work now, it was just the thing that if it went HIGH so the button was pressed for more than 2s it didnt reset the "time" and "time2" so the if(time2-time > holdtime) was running all the time, so i just made correction, whenever it assign a different state, it resets the "time" and "time2".

still there it doesnt something work good for me, and that is unsigned long for "time" and "time2". If i make only long type it works fine but with unsigned long it goes on right after pushing button... long gives me enough space for millis() but still, what is different with unsigned long?
13  Forum 2005-2010 (read only) / Troubleshooting / long hold mode on: July 08, 2010, 09:16:59 am
Hi
Im new in programming and also with Arduino. So i gave myself a first challenge to make a a button mode - long hold.
I wrote that code but dont know what is wrong with it because it allll seeeemmmss soooo perfect to me smiley-grin

the point is, if you hold down the button for 2s or more the light will go HIGH, and if it is allready HIGH it will go LOW... this is the first step, later on i will try to make some doubleclick feature...

Code:

int ledpin = 2;
int buttonpin = 5;
int buttonstate = 0;
int exstate = 0;
int ledstate = 0;
int time = 0;
int time2 = 0;
int timedown = 2000;

void setup () {
pinMode(ledpin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
}

void loop () {

buttonstate = digitalRead(buttonpin);
if(buttonstate == HIGH && exstate == LOW) {           //remember the time the button was pressed
time = millis();
}

if(buttonstate == HIGH && exstate == HIGH) {           //counts how long the button is down
time2 = millis();
}

if((time2 - time) > timedown) {                               //if the button is pressed for more than 2s the change in ledstate will happens
  if(ledstate == 0){
  ledstate = 1;
  }
  else{
  ledstate = 0;
  }
}

if(ledstate == 0) {
digitalWrite(ledpin, LOW);
}

if(ledstate == 1) {
digitalWrite(ledpin, HIGH);
}


exstate = buttonstate;
}


Pages: [1]