Show Posts
Pages: [1] 2 3 ... 7
1  Using Arduino / Programming Questions / Re: solved: simple loop logic quesiton on: December 23, 2013, 06:09:01 pm
No, it just screws up what is stored in SRAM so your sketch does funny things; some other variable is getting its stored value bushwhacked.

I see, so it is something I need to fix. even it has not giving me an error yet.
2  Using Arduino / Programming Questions / Re: logical timing on: December 23, 2013, 06:07:37 pm
If you want the button state to be available to all functions in your program declare it as global.  Is that what you mean ?

I think my button is global. I want every function to interpret the button differently, and I don't want to write every button code for every function. Is there a simpler way to do that?
3  Using Arduino / Programming Questions / Re: logical timing on: December 23, 2013, 03:35:49 pm
Quote
you kinda confuse me more. so,  I need to write code to detect changes from LOW to HIGH, but not just HIGH?
Yes.  You need to do the increment when the button state becomes HIGH, not when it is HIGH otherwise the increment will happen every time through loop() if the button state is HIGH.

thanks...then, I need to write every button code for each function??
4  Using Arduino / Programming Questions / Re: solved: simple loop logic quesiton on: December 23, 2013, 03:33:54 pm
If you ever set SC to zero then SC-1 is not a valid array index. Even if you don't see any symptoms yet, it's likely that this is corrupting memory and not working as you think.

will this corrupting memory damage my naon board in long term?
5  Using Arduino / Programming Questions / Re: logical timing on: December 23, 2013, 08:57:12 am
You must do the increment in the code that tests cButtonA != pButtonA etc.

The whole point is to only increment when the button state changes, not every time
round the loop().  If you set a variable saying "the button changed" then that will continue
to read true in the future, you need to execute your state-change code when the state
changes, not if you've seen it change at some point in the past.

This is the idiom to use, don't change it to something different!
Code:
 cButtonA = digitalRead (....) ;
  if (cButtonA == LOW && pButtonA == HIGH)  // button pin transition to LOW
  {
    count++ ;
  }
  pButtonA = cButtonA ;

[ you probably also need debouncing code ]

you kinda confuse me more. so,  I need to write code to detect changes from LOW to HIGH, but not just HIGH?
6  Using Arduino / Programming Questions / logical timing on: December 23, 2013, 07:47:23 am
I have some doubts about logical timing, or whatever it's called.
For example,
A=1, 
if A==1, then aCount++;

what will happen to this aCount value? will it keep going up indefinitely?

I have this code to set sbA HIGH when button A is pressed, and once released, sbA goes LOW;
Code:
if (cbuttonA != pbuttonA){
if(cbuttonA==LOW){     // buttonA
sbA=1;
}
else sbA=0;
}
pbuttonA=cbuttonA;


and I have this code to count the button, but it doesn't work.
Code:
if(sbA==1){
count++;} 

 I know I can put count++ directly to the button code above, but I am writing a program that have different modes and so the buttons have different functions. essentially,  I am try to make an universal button state value, so that every mode can use them in different ways. any suggestion?
7  Using Arduino / Programming Questions / Re: simple loop logic quesiton on: December 23, 2013, 06:26:58 am
Quote
what is the problem here?
 when SC=10, then SC-1=9, then stLED[9]=0 should go LOW

SC never has a chance to be decremented to 9, you set it to zero then subtract one:

Quote
if(SC==10){
SC=0;
stLED[9]=0;}

stLED[SC-1]=0;
stLED[SC]=1;

I see.  thanks, it works like this.

Code:
if(mode==0){
stLED[SC-1]=0;

if(SC==10){
SC=0;
}

stLED[SC]=1;
8  Using Arduino / Programming Questions / solved: simple loop logic quesiton on: December 23, 2013, 06:12:41 am
I have a simple question about loop. see codes. when SC=10, then SC-1=9, then stLED[9]=0 should go LOW. but it's not the case here. I have to write this line "stLED[9]=0;" to get stLED[9] low. what is the problem here?

this works
Code:

if(SC==10){
SC=0;
stLED[9]=0;}

stLED[SC-1]=0;
stLED[SC]=1;

this doesn't work
Code:
if(SC==10){
SC=0;
}

stLED[SC-1]=0;
stLED[SC]=1;
9  Using Arduino / Programming Questions / Re: program doesn't work, need help in finding the bug on: December 18, 2013, 03:05:06 pm
You have to write something (HIGH or LOW) to pin 13.

thank you. This is old code reused. I have that on my old code which I deleted accidentally when editing this new one. I was concentrating on the code for the LED sequences. It doesn't work also.

is there anything wrong with this? all led lighted up in this case. I need them to light up according to the sec in a sequence.
Code:
for(int x=0; x<=9; x++){
if(milsec%10==1){
stLED[x]=1;
}
}


Code:
unsigned long timer= millis();
 if(timer-pstimer > 100){
   milsec++;
   miniTimer=!miniTimer;   
   pstimer = timer;
 } 
 
 
 if(milsec==10){
  stLEDf=!stLEDf; // 13 pin sec flasher
  sec++; // add 1 sec to sec
  milsec=0;
 
 }


if(mode==0){

for(int x=0; x<=9; x++){
if(milsec%10==1){
stLED[x]=1;
}
}
}
10  Using Arduino / Programming Questions / Re: program doesn't work, need help in finding the bug on: December 18, 2013, 02:36:18 pm
It is impossible to determine what your functions actually are.   You seem to have a whole pile of code there which is not in any function.

I just need pin 13 to go high and low every sec to flash the LED.
11  Using Arduino / Programming Questions / program doesn't work, need help in finding the bug on: December 18, 2013, 02:15:10 pm

this part didn't run. led 13 didn't flash.
Code:
if(milsec==10){
  stLEDf=!stLEDf; // 13 pin sec flasher
  sec++; // add 1 sec to sec
  milsec=0;
 
 }

here is the logic. is there any problem to this?
Code:
unsigned long timer= millis();
 if(timer-pstimer > 100){
   milsec++;
   miniTimer=!miniTimer;   
   pstimer = timer;
 } 
 
 
 if(milsec==10){
  stLEDf=!stLEDf; // 13 pin sec flasher
  sec++; // add 1 sec to sec
  milsec=0;
 
 }


here are Full code
Code:
int milsec=0;
int sec=0;
int minu=0;
int hr=0;

unsigned long pstimer=0;

int pbuttonA=1;
int pbuttonB=1;
int pbuttonC=1;
int pbuttonD=1;

int ledA=8;
int ledB=9;
int ledC=10;
int ledD=11;
int ledE=12;
int ledF=13;

int stLEDf=0;

int stLED[10]={0,0,0,0,0,0,0,0,0,0};

int mode=0; // 0 for sec counting; 1 for clock mode;
int set_time=0; // 0 for nothing; 1 for setting hr; 2 for setting minu.
int miniTimer=0; // 1/10 a sec minitimer

void setup() { 
 
pinMode(A2,INPUT_PULLUP); //buttonA
pinMode(A3,INPUT_PULLUP); //buttonB
pinMode(A4,INPUT_PULLUP); //buttonC
pinMode(A5,INPUT_PULLUP); //buttonD

pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

}

void loop() {

//---------------------------------------------------------------------------------- 
//======= Start Button ================================================================== 
int cbuttonA=digitalRead (A2); 
int cbuttonB=digitalRead (A3);
int cbuttonC=digitalRead (A4);
int cbuttonD=digitalRead (A5);

//buttonA//
if (cbuttonA != pbuttonA){
if(cbuttonA==LOW){     // buttonA
hr++;
}
}
pbuttonA=cbuttonA;

//buttonB//
if (cbuttonB != pbuttonB){
if(cbuttonB==LOW){            // buttonB
minu++;
}
}
else (digitalWrite (9, LOW));
pbuttonB=cbuttonB;

//buttonC//
if (cbuttonC != pbuttonC){
if (cbuttonC==LOW){           // buttonC
set_time++;
if(set_time==3) set_time=0;
}
}
pbuttonC=cbuttonC;


//buttonD//
if (cbuttonD != pbuttonD){
if (cbuttonD==LOW){           // buttonD
mode=!mode;
}
}
pbuttonD=cbuttonD;

//button D C//


//----------------------------------------------------------------------------------
//======= End button ===============================================================


//----------------------------------------------------------------------------------
//======= start timer ==============================================================
unsigned long timer= millis();
 if(timer-pstimer > 100){
   milsec++;
   miniTimer=!miniTimer;   
   pstimer = timer;
 } 
 
 
 if(milsec==10){
  stLEDf=!stLEDf; // 13 pin sec flasher
  sec++; // add 1 sec to sec
  milsec=0;
 
 }
//----------------------------------------------------------------------------------
//=======End timer =================================================================


//----------------
if(mode==0){

for(int x=0; x<=9; x++){
if(milsec%10==1){
stLED[x]=1;
}
}
}

//----------------




//----------------------------------------------------------------------------------
//======= start LED Code ==========================================================

if(stLED[0]==1){
digitalWrite (ledA, LOW);
digitalWrite (ledB, LOW);
digitalWrite (ledC, LOW);
digitalWrite (ledD, LOW);
digitalWrite (ledE, LOW);
}

if(stLED[1]==1){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, LOW);
digitalWrite (ledC, LOW);
digitalWrite (ledD, LOW);
digitalWrite (ledE, LOW);
}

if(stLED[2]==1){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, LOW);
digitalWrite (ledD, LOW);
digitalWrite (ledE, LOW);
}

if(stLED[3]==1){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, LOW);
digitalWrite (ledE, LOW);
}

if(stLED[4]==1){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, LOW);
}

if(stLED[5]==1){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
}

if(stLED[6]==1){
digitalWrite (ledA, LOW);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
}

if(stLED[7]==1){
digitalWrite (ledA, LOW);
digitalWrite (ledB, LOW);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
}

if(stLED[8]==1){
digitalWrite (ledA, LOW);
digitalWrite (ledB, LOW);
digitalWrite (ledC, LOW);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
}

if(stLED[9]==1){
digitalWrite (ledA, LOW);
digitalWrite (ledB, LOW);
digitalWrite (ledC, LOW);
digitalWrite (ledD, LOW);
digitalWrite (ledE, HIGH);
}


//----------------------------------------------------------------------------------
//======= End LED Code ============================================================


























}
12  Using Arduino / Programming Questions / Re: Need help in finding a tiny bug - Nano on: December 12, 2013, 08:15:06 am
Maybe this little caveat on the Nano web page might shed some light on the issue:
Quote
Analog pins 6 and 7 cannot be used as digital pins.

.....that ..... thank you
13  Using Arduino / Programming Questions / Re: Need help in finding a tiny bug - Nano on: December 12, 2013, 01:29:44 am
Code:
 if(timer-pstimer > 1000){
    secLED=!secLED;
    sec++;
    pstimer = timer;
  }  

How does that loop ever exit?
What loop?

There is nothing I can see that is wrong with your code - it works (with a few pin number tweaks) on my Uno.  Ergo it is most probably either your wiring or a duff Nano.

I just use serial to test my board. and it works fine.

Code:
int pinA=A6;
int pinB=A7;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins input:
  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);

}

void loop() {
  // if there's any serial available, read it:
  if (Serial.available() > 0) {
      int testA=analogRead (pinA);
      int testB=analogRead (pinB);

      // print the pin
      Serial.print("PINA:");
      Serial.println(testA);
      delay (500);
      Serial.print("PINB:");     
      Serial.println(testB);
      delay (500);     
    }
  }






Code:
PINB:277
PINA:137
PINB:0
PINA:89
PINB:0
PINA:105
PINB:0
PINA:96
PINB:0
PINA:112
PINB:68
PINA:0
PINB:85
PINA:0
PINB:87
PINA:0
PINB:92
PINA:0
PINB:91
PINA:0
PINB:89
PINA:0
PINB:94
PINA:0
PINB:89
PINA:0
PINB:92
PINA:0
PINB:93
PINA:0
PINB:89
PINA:86
PINB:51
PINA:87
PINB:0
PINA:91
PINB:0
PINA:106
PINB:0
PINA:83
PINB:0
PINA:0
PINB:62
PINA:0
PINB:85
PINA:168
PINB:175
PINA:253
14  Using Arduino / Programming Questions / Re: Need help in finding a tiny bug - Nano on: December 12, 2013, 01:09:57 am
Code:
 if(timer-pstimer > 1000){
    secLED=!secLED;
    sec++;
    pstimer = timer;
  }  

How does that loop ever exit?
What loop?

There is nothing I can see that is wrong with your code - it works (with a few pin number tweaks) on my Uno.  Ergo it is most probably either your wiring or a duff Nano.

maybe because I have a damaged nano board??
15  Using Arduino / Programming Questions / Re: Need help in finding a tiny bug - Nano on: December 12, 2013, 01:08:40 am
I am not sure what you really mean by right type, but I am definitely sure all my LEDs wire correctly.
Not sure int is the right type for ledAS through ledDS and secLED. Iwould probably have used bool.

However, that wouldn't explain why 2 out of the 4 LEDs wook, and 2 don't. Are you sure you have LED C and LED D wired from the pins to GND?

Pages: [1] 2 3 ... 7