Outils pour utilisateurs

Outils du site


mon_premier_robot
/* code en vrac */
 
//1/DETECTION IR
/*led connected to digital pin 11 et 12
*the IR is connected to analog pin A0 < devient 2
*/
//4
/*SERVO OUT en 9 et potentiometre identique au SENSOR en A2
* Controlling a servo position using a potentiometer (variable resistor)
*/
//************************LIBRARY*************************
#include <Servo.h>
//************************OBJECT*************************
Servo myservo1;// create servo object to control a servo 
Servo myservo2;
//************************CONSTANTE*************************
//1/constante de l'IR
const int IRconnect = 2;      // reception IR en 2. + sur 5v et - sur neutre commun
const int led1 = 13; // led verte sur 13
const int led2 = 12;  // led rouge sur 12
 
//3/constante du SENSOR : RAS
//4/constante du SERVO
//const int on = 90;
//const int off = 0;
int pos = 0;    // variable to store the servo position 
// reads the value of the potentiometer (value between 0 and 1023)
//*************************VARIABLE SERVO************************
//int potpin = A2;  // analog pin used to connect the potentiometer en A3
//potpin identique a sensorPin 
int val;    // variable to read the value from the analog pin 
int ledPin = 12;
int speakerOut = 3;    
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody1[] = "2d2a";
byte melody2[] = "1f2c";
byte melody3[] = "2d2a";
byte melody4[] = "2c2f";
//byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 10;
int statePin = LOW;
//*******EAU
int ledPinEau = 7;
int Eau = 11;
//*******SON id EAU
int ledPinSon = 7;
int valueSon = 400;
//********TEMPERATURE
int a;
int del = 1000;
float temperature;
int B=3975;
float resistance;
//******Vibreur
int VibPin=10;
//******TOUCH id LED EAU
const int TouchPin=9;
const int LedPinTouch=12;
//=======================================================
void setup() {
//*************************SETUP IR*************************
 pinMode(IRconnect, INPUT); // connnection IN IR
 
 digitalWrite(IRconnect, HIGH); // 
 
 pinMode(led1, OUTPUT); // led 1 verte
 pinMode(led2, OUTPUT); // led 2 rouge
//*************************SETUP SERVO*************************
 myservo1.attach(6);  // attaches the servo on pin 6 to the servo object 
 myservo2.attach(5);
 
 pinMode(ledPin, OUTPUT);
 
//***********EAU
 pinMode(ledPinEau, OUTPUT);
 pinMode(Eau, INPUT);
//**********SON - id EAU
 pinMode(ledPinSon, OUTPUT);
 
//*****Touch
pinMode(TouchPin,INPUT);
pinMode(LedPinTouch,OUTPUT);
 
//******TEMP
Serial.begin(9600);
//******Vibreur
pinMode(VibPin, OUTPUT);
}
//=========================================================
void loop() {
//*************************LOOP IR*************************
  digitalWrite(LedPinTouch,HIGH);
//************
//************touch sensor
  digitalWrite(LedPinTouch,HIGH);
int sensorValue = digitalRead(TouchPin);
if (sensorValue==1)
{   
  digitalWrite(LedPinTouch,LOW);
    //*************ZIC
//analogWrite(speakerOut, 0);    
  for (count = 0; count < MAX_COUNT; count++) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    for (count3 = 0; count3 <= (melody4[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody4[count*2 + 1]) {      
          analogWrite(speakerOut,500);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          delayMicroseconds(tones[count2]);
                                                    }
        if (melody4[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(500);
                                          }
                                        }
                                                                        }
                                                }
  }
 
else {
  digitalWrite(LedPinTouch,HIGH);
      }
 
//************
  if (digitalRead(IRconnect))
  { // detection IR
 
    digitalWrite(led1, HIGH); // led rouge allumée
    digitalWrite(led2, LOW); // led verte eteinte
//**************SERVO1 - bras
//  for(pos = 0; pos < 20; pos += 1)  // goes from 0 degrees to 180 < 25 degrees 
//  {                   // in steps of 1 degree 
    int pos1 = 90;
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
//  } 
//*************SERVO2 - bouche
//  for(pos = 0; pos < 14; pos += 1)  // goes from 0 degrees to 180 < 25 degrees 
//  {       // in steps of 1 degree 
    int pos3 = 0;
    myservo2.write(pos3);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
//  }
     int pos2 = 5;
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
    delay(15);        // waits 15ms for the servo to reach the position 
 
    int pos4 = 70;
    myservo1.write(pos4);              // tell servo to go to position in variable 'pos' 
    delay(15);           
  }
 
//**************
 
    else
  { // pas de detection
    digitalWrite(led1, LOW); // led rouge eteinte
    digitalWrite(led2, HIGH); // led verte allumée
//*************************LOOP SERVO*************************
                                    //---  delay(100);  // delay to avoid overloading the serial port buffer
  }
 
//*********EAU
 int eau = 0;
 eau = digitalRead(11);
//  rain = analogRead(1);
digitalWrite(ledPinEau, statePin);
  if(eau == LOW){
    digitalWrite(ledPinEau, HIGH); // led rouge allumée
    //*************ZIC
//analogWrite(speakerOut, 0);    
  for (count = 0; count < MAX_COUNT; count++) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    for (count3 = 0; count3 <= (melody1[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody1[count*2 + 1]) {      
          analogWrite(speakerOut,500);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          delayMicroseconds(tones[count2]);
        }
        if (melody1[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
    }
                                                }
//***********
digitalWrite(VibPin,HIGH);
delay(1000);
 
digitalWrite(VibPin,LOW);
delay(1000);
 
  }               
}
 
 
 
mon_premier_robot.txt · Dernière modification: 2015/01/28 03:09 (modification externe)