2016年6月18日 星期六

// Sensor sequence: SLeftLeft SMiddle SRightRight
#include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13);  //Trige,Echo
int ulval;

const int SLeftLeft = 5;      //左感測器輸入腳
const int SMiddle = 6;     //中間感測器輸入腳
const int SRightRight = 7;     //右感測器輸入腳

// variables will change:

int SLL;    //左感測器狀態
int SM;    //中感測器狀態
int SRR;    //右感測器狀態

const int Motor_M1a = 13;    //R
const int Motor_M1b = 12;
const int Motor_E1 = 10;
const int Motor_M2a = 11;   //L
const int Motor_M2b = 8;  
const int Motor_E2 = 9;

byte byteSensorStatus=0;
int speed=220;

#define SENSOR_L 4;
#define SENSOR_M 2;
#define SENSOR_R 1;

void setup() {

   //set up serial communications

   Serial.begin(9600);

  // 輸出入接腳初始設定

  pinMode(SLeftLeft, INPUT);

  pinMode(SMiddle, INPUT);

  pinMode(SRightRight, INPUT);
  pinMode(Motor_M1a, OUTPUT);
  pinMode(Motor_M1b, OUTPUT);
  pinMode(Motor_M2a, OUTPUT);
  pinMode(Motor_M2b, OUTPUT);
  pinMode(Motor_E1,OUTPUT);
  pinMode(Motor_E2,OUTPUT);
}



void loop(){

 looptest();
 delay(3000);
/*
  ulval=ultrasonic.Ranging(CM);
   if (ulval < 10){
  forward(0,255);
  delay(2000);
  back(0,255);
  delay(2000);
  right(0,255);
  delay(2000);
  left(0,255);
  delay(2000);
  motorstop(0,0);
  delay(3000);

 
    }
*/



}

void looptest(){

  forward(0,255);

  delay(2000);

  back(0,255);

  delay(2000);

  right(0,255);

  delay(2000);

  left(0,255);

  delay(2000);

  motorstop(0,0);

  delay(3000);

}

void motorstop(byte flag, byte numOfValues)

{

  digitalWrite( Motor_E1, 0);

  digitalWrite( Motor_E2, 0);

//  Serial.println("stop : ");

}

void forward(byte flag, byte numOfValues)

{

  digitalWrite( Motor_M1a, LOW);
  digitalWrite( Motor_M1b, HIGH);

  digitalWrite( Motor_M2a, HIGH);
  digitalWrite( Motor_M2b, LOW);

  analogWrite( Motor_E1, numOfValues);

  analogWrite( Motor_E2, numOfValues);

}

void back(byte flag, byte numOfValues)

{

 digitalWrite( Motor_M1a, HIGH);
  digitalWrite( Motor_M1b, LOW);

  digitalWrite( Motor_M2a, LOW);
  digitalWrite( Motor_M2b, HIGH);


  analogWrite( Motor_E1, numOfValues);

  analogWrite( Motor_E2, numOfValues);

}

void right(byte flag, byte numOfValues)

{

  digitalWrite( Motor_M1a, HIGH);
  digitalWrite( Motor_M1b, LOW);

  digitalWrite( Motor_M2a, HIGH);
  digitalWrite( Motor_M2b, LOW);


  analogWrite( Motor_E1, numOfValues);

  analogWrite( Motor_E2, numOfValues);

}

void left(byte flag, byte numOfValues)

{

  digitalWrite( Motor_M1a, LOW);
  digitalWrite( Motor_M1b, HIGH);

  digitalWrite( Motor_M2a, LOW);
  digitalWrite( Motor_M2b, HIGH);
 
 analogWrite( Motor_E1, numOfValues);

  analogWrite( Motor_E2, numOfValues);

}

2016年6月17日 星期五

7/16比賽用音樂

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_A7, NOTE_B7, NOTE_A7, NOTE_A7, NOTE_B7, 0, NOTE_B7, NOTE_G7
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 8, 4, 8, 8, 4, 8
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
   for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

馬達

SERVO馬達

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0控制位子的地方;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9)馬達的位子;  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

2016年5月21日 星期六

專題

姓名:巫守原 李柏陞
功能:防小偷 和整小偷
程式:  
#include <Ultrasonic.h>
#include <Servo.h>
Servo myservo;
const int buttonPin = 2;  
const int ledPin =  4;    
const int ledPin2 =  3;
 int val;
 int potpin = 0;
int buttonState = 0;        
Ultrasonic ultrasonic(12,13); // (Trig PIN,Echo PIN)
void setup() {
 myservo.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
   Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}
void loop() {
 buttonState = digitalRead(buttonPin);
 Serial.print(ultrasonic.Ranging(CM)); // CM or INC
  Serial.println(" cm" );
 if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
    delay(10000);
    int val = ultrasonic.Ranging(CM);
    if(val<=30){
    myservo.write(180);
  }
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    }
}

2016年5月20日 星期五

開關+燈(不用一直按)

按鈕



// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2按鈕的位子;    // the number of the pushbutton pin
const int ledPin = 5燈的位子;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

Arrays 紅綠燈


int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = {
  3, 4, 5, LED燈放的接腳
};       // an array of pin numbers to which LEDs are attached
int pinCount = 3;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  } // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);


  }

int timerr[] ={ 10000,3500,9500}停的秒數(綠黃紅);             // The higher the number, the slower the timing.
int ledPins[] = {
    3,4,5
};       // an array of pin numbers to which LEDs are attached
int pinCount = 3;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timerr[thisPin]);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }

  // loop from the highest pin to the lowest:
  //for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    //digitalWrite(ledPins[thisPin], HIGH);
   // delay(timer);
    // turn the pin off:
   // digitalWrite(ledPins[thisPin], LOW);
  }





Arduino聲音 MELODY
 */
#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};聲音(音階)C(DO) D(RE)E(MI)F(FA)G(SO)A(LA)B(SI)

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};節拍

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++)為上方聲音的順序 {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8時間(1000單位為1秒), etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.不會重覆