User:Inge Hoonte/arduino notes oct 26: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 28: Line 28:




for (x=0; x<10; x++){
for (x=0; x<10; x++){
 
for ((i=0; i<3; i++)) {
for ((i=0; i<3; i++)) {
something;
something;
something
something
}
}




Line 43: Line 43:
char = a var
char = a var
pat = pattern (also a variable name that you define)
pat = pattern (also a variable name that you define)
[ ] means that a list of variables follows  
[ ] means that a list of variables follows  
speakerPin (P is capital, camel case)
speakerPin (P is capital, camel case)
; separates each statement
; separates each statement
Line 49: Line 49:




int speakerPin = 13;
int speakerPin = 13;
//char keycode = 'x'
//char keycode = 'x'
char pat[] = "xxx xx x xx ";
char pat[] = "xxx xx x xx ";
   
   
void setup() {}
void setup() {}
    
    
void loop () {
void loop () {
   
   
         if (pat[0] == 'x') {
         if (pat[0] == 'x') {
Line 62: Line 62:
         delay 1000
         delay 1000
     }
     }
}
}
   
   


Line 75: Line 75:




int speakerPin = 13;
int speakerPin = 13;
int thedelay = 1000
int thedelay = 1000
//char keycode = 'x'
//char keycode = 'x'
char pat[] = "xxx xx x xx ";
char pat[] = "xxx xx x xx ";
   
   
void setup() {}
void setup() {}
    
    
void loop () {
void loop () {
   int i = 0;
   int i = 0;
    
    

Revision as of 17:49, 26 October 2010

int speakerPin = 13;
int thedelay = 100;
//char keycode = 'x'
char pat[] = "xxx xx x xx ";
int patlen = strlen(pat);

void setup() {}
 
void loop () {
 int i = 0;
 while (i<strlen(pat)) {
    if (pat[i] == 'x')   tone(speakerPin, 400, 10);
    delay(thedelay);
    i = i+1;
 }
  //can be written in shorthand:
  /*
  for (i=0; i<12; i+=1)
    if (pat[i] == 'x')   tone(speakerPin, 400, 10);
    delay(thedelay);
   
       }
       */
       
       delay (thedelay*10);
   }


for (x=0; x<10; x++){

for ((i=0; i<3; i++)) {
something;
something
}


void = return value C doesn't read emptiness

int [name] char = a var pat = pattern (also a variable name that you define) [ ] means that a list of variables follows speakerPin (P is capital, camel case)

separates each statement

// if you put it before a code, it will not run


int speakerPin = 13;
//char keycode = 'x'
char pat[] = "xxx xx x xx ";

void setup() {}
 
void loop () {

       if (pat[0] == 'x') {
           tone(speakerPin, 500, 10);
       }
       delay 1000
   }
}

if pat [0] it starts at first x = is assignment statement: what's on left gets assigned to what's on right == asks the q if these two things are equal. answer is true or false.

tone (output, toonhoogte in Hz, milisec duration)

NOW embed the pattern! while is an if that can go on and on


int speakerPin = 13;
int thedelay = 1000
//char keycode = 'x'
char pat[] = "xxx xx x xx ";

void setup() {}
 
void loop () {
  int i = 0;
  
 while (i<12) {
  if (pat[i] == 'x')   tone(speakerPin, 400, 10);
  delay(thedelay);
  i = i+1;
   
       }
       delay (thedelay*20);
   }

you have a while loop the variable goes in a formula you establish the future of i end of loop

for (i=0; i<12; i+=1)

strlen(pat) = counts the length of the defined pattern automatically