Demo entry 4784643

jingzhi

   

Submitted by anonymous on May 17, 2016 at 16:23
Language: Arduino. Code size: 4.3 kB.

#include <avr/sleep.h>
#define NUM_READS 100
const int SL = A3;
const int SR = A1;
const int ML = 0;
const int MR = 1;
const int EXTRA = 4;
const int WHITE=165;
const int BLACK=780;
const int C  = 239;
const int CS = 225;
const int D  = 213;
const int DS = 201;
const int E  = 190;
const int F  = 179;
const int FS = 169;
const int G  = 159;
const int GS = 150;
const int A  = 142;
const int AS = 134;
const int B  = 127;

void setup() {
  pinMode(SL, INPUT);
  pinMode(SR, INPUT);
  pinMode(ML, OUTPUT);
  pinMode(MR, OUTPUT);
  pinMode(EXTRA, OUTPUT);
 
}
void loop() {
 tone1();
 follow();
 halt();
 play(C, 5,500);
 delay(100);
 play(E, 5,500);
 delay(100);
 play(G, 5,500);
 delay(100);
 straight();
 halt();
 tone2();
 spiral();
 tone3();
 while(true)
 {
  halt();
  }

}

void goleft() {
  analogWrite(MR, 75);
  digitalWrite(ML, LOW);
  return;
}
void goright() {

  analogWrite(ML, 80);
  digitalWrite(MR, LOW);
}
void goforward() {
  analogWrite(ML,80);
  analogWrite(MR,75);
}
void halt(){
  digitalWrite(ML,LOW);
  digitalWrite(MR,LOW);
  }

void follow(){
  int white=500;
  bool stage1=true;
  int count = 0;
  int sensorLlast = readsensor(SL);
  int sensorRlast = readsensor(SR);
  while (stage1) {
    int sensorL = readsensor(SL);
    int sensorR = readsensor(SR);
    if (sensorL < white && sensorR > white) //sensorL sees white, sensor R sees black
      {
      goright();     //red
    }
   else if (sensorR < white && sensorL > white) //sensorR sees white, sensorL sees black
      {
       goleft();  //green
    }
    else {
      goforward();
    }
     if(sensorL<white && sensorR<white)
    {
      count++;
      halt();
      goforward();
      if(count>100)
      {
       halt();
        }
      stage1=false;
    }
    
  }
}
void straight(){
   analogWrite(ML,82);
  analogWrite(MR,73);
  delay(2560);
  digitalWrite(ML,LOW);
  digitalWrite(MR,LOW);
  delay(3000);}

void spiral(){
 analogWrite(ML, 80);
 delay(2000);
 digitalWrite(ML, LOW);
 analogWrite(ML, 80);
 analogWrite(MR, 110);
 for (int i = 0; i <60; i++) {
  check();
 analogWrite(ML, 75-i);
 analogWrite(MR, 110+i);
 delay(200); 
  }
  digitalWrite(ML, LOW);
  digitalWrite(MR, LOW);
}  

void tone1(){
 play(C, 6, 500);
 delay(500);
 play(C, 6, 500);
 delay(500);
 play(C, 6, 500);
 delay(500);
 play(C, 5,800);
 delay(100);
}

void tone2(){
 play(C, 5,500);
 delay(100);
 play(E, 5,500);
 delay(100);
 play(G, 5,500);
 delay(100);
 play(C,6,500);
 delay(100);
}

void tone3()
{
 play(E, 5, 300);
 delay(50);
 play(DS, 5, 300);
  delay(50);
 play(E, 5, 300);
  delay(50);
 play(DS, 5, 300);
  delay(50);
 play(E, 5, 300);
  delay(50);
 play(B, 4, 300);
  delay(50);
 play(D, 5, 300);
 delay(50);
 play(C, 5, 300);
 delay(50);
 play(A, 4, 500);
 delay(50);
 
 play(C, 4, 300);
 delay(50);
 play(E, 4, 300);
 delay(50);
 play(A, 4, 300);
 delay(50); 
 play(B, 4, 500);
 delay(50);
 
 play(E, 4, 300);
 delay(50);
  play(GS, 4, 300);
 delay(50);
  play(B, 4, 300);
 delay(50);
  play(C, 5, 500);
 delay(50);
}
void play(unsigned char divisor, unsigned char octave, unsigned long duration)
{
  TCCR1 = B10000000| (11-octave); 
  GTCCR = B00010000;
  OCR1C = divisor-1;         
  delay(duration);
  TCCR1 = B10000000;         
}
float readsensor(int sensorpin){
   // read multiple values and sort them to take the mode
   int sortedValues[NUM_READS];
   for(int i=0;i<NUM_READS;i++){
     int value = analogRead(sensorpin);
     int j;
     if(value<sortedValues[0] || i==0){
        j=0; //insert at first position
     }
     else{
       for(j=1;j<i;j++){
          if(sortedValues[j-1]<=value && sortedValues[j]>=value){
            // j is insert position
            break;
          }
       }
     }
     for(int k=i;k>j;k--){
       // move all values higher than current reading up one position
       sortedValues[k]=sortedValues[k-1];
     }
     sortedValues[j]=value; //insert current reading
   }
   //return scaled mode of 10 values
   float returnval = 0;
   for(int i=NUM_READS/2-5;i<(NUM_READS/2+5);i++){
     returnval +=sortedValues[i];
   }
   returnval = returnval/10;
   return returnval;
}

void check(){
  int black = 700;
  int sensorL = readsensor(SL);
  int sensorR = readsensor(SR);
  if(sensorL > black && sensorR >black)
  {
    halt();
    while(true){
      ;
    }
    }
  }

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).