User:Inge Hoonte/arduino notes oct 26

From XPUB & Lens-Based wiki
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