Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TASK ONE
- /*
- Blink
- Turns on an LED on for one second, then off for one second, repeatedly.
- This example code is in the public domain.
- */
- // Pin 13 has an LED connected on most Arduino boards.
- // give it a name:
- int led = 13;
- // the setup routine runs once when you press reset:
- void setup() {
- // initialize the digital pin as an output.
- pinMode(led, OUTPUT);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(1000); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- }
- /*
- Button
- Turns on and off a light emitting diode(LED) connected to digital pin 13,
- when pressing a pushbutton attached to pin 2.
- The circuit:
- - LED attached from pin 13 to ground
- - pushbutton attached to pin 2 from +5V
- - 10K resistor attached to pin 2 from ground
- - Note: on most Arduinos there is already an LED on the board
- attached to pin 13.
- created 2005
- by DojoDave <http://www.0j0.org>
- modified 30 Aug 2011
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/Button
- */
- // 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 = 13; // the number of the LED pin
- // variables will change:
- int buttonState = 0; // variable for reading the pushbutton status
- void setup() {
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- }
- void loop() {
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
- // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
- if (buttonState == HIGH) {
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- } else {
- // turn LED off:
- digitalWrite(ledPin, LOW);
- }
- }
- //Controlling led by potentiometer
- //Rotate the shaft of the potentiometer and you should see the luminance of the LED change.
- //Email:support@sunfounder.com
- //Website:www.sunfounder.com
- //2015.5.7
- /******************************************/
- const int analogPin = A0;//the analog input pin attach to analog pin A0
- const int ledPin = 9;//the led attach to pin 9
- int inputValue = 0;//variable to store the value coming from sensor
- int outputValue = 0;//variable to store the output value
- /******************************************/
- void setup()
- {
- }
- /******************************************/
- void loop()
- {
- inputValue = analogRead(analogPin);//read the value from the sensor
- outputValue = map(inputValue,0,1023,0,255);//Convert from 0-1023 proportional to the number of a number of from 0 to 255
- analogWrite(ledPin,outputValue);//turn the led on depend on the output value
- }
- /*******************************************/
- //TASK TWO
- /*
- * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
- *
- * by Dejan Nedelkovski,
- * www.HowToMechatronics.com
- *
- */
- // defines pins numbers
- const int sensorPin = 0;
- const int buzzerPin = 23;
- //const int ledPin = 9;
- // We'll also set up some global variables for the light level:
- int lightLevel, high = 0, low = 1023;
- const int trigPin = 9;
- const int echoPin = 10;
- int motorPin = 3;
- // defines variables
- long duration;
- int motorspeed;
- int distance;
- void setup() {
- //pinMode(ledPin, OUTPUT);
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- Serial.begin(9600); // Starts the serial communication
- pinMode(motorPin, OUTPUT);
- pinMode(buzzerPin,OUTPUT);
- while (! Serial);
- Serial.println("Speed 0 to 255");
- }
- void loop() {
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance= duration*0.034/2;
- if (distance > 25) {
- distance = 25;
- }
- if(distance > 1000) {
- distance = 0;
- }
- int speed = distance*10;
- if (speed >= 0 && speed <= 255)
- {
- analogWrite(motorPin, speed);
- Serial.println("speed");
- Serial.println(speed);
- }
- // Prints the distance on the Serial Monitor
- delay(1000);
- lightLevel = analogRead(sensorPin);
- //delay(1000);
- manualTune(); // manually change the range from light to dark
- if(lightLevel < 100) {
- //tone(buzzerPin,HIGH);
- //delay(1000);
- digitalWrite(buzzerPin,HIGH);
- //delay(1000);
- }
- else {
- //tone(buzzerPin,500);
- //delay(1000);
- digitalWrite(buzzerPin,LOW);
- //delay(1000);
- }
- //analogWrite(ledPin, lightLevel);
- Serial.println("lightLevel");
- Serial.println(lightLevel);
- //Serial.print("Distance: ");
- //Serial.println(distance);
- }
- void manualTune()
- {
- lightLevel = map(lightLevel, 0, 1023, 0, 255);
- lightLevel = constrain(lightLevel, 0, 255);
- }
- void autoTune()
- {
- if (lightLevel < low)
- {
- low = lightLevel;
- }
- if (lightLevel > high)
- {
- high = lightLevel;
- }
- lightLevel = map(lightLevel, low+30, high-30, 0, 255);
- lightLevel = constrain(lightLevel, 0, 255);
- }
- //TASK THREE
- #include<Wire.h>
- const int MPU=0x68;
- int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
- #define ADC_ref 5
- #define zero_x 1.799
- #define zero_y 1.799
- #define zero_z 1.799
- #define sensitivity_x 0.4
- #define sensitivity_y 0.4
- #define sensitivity_z 0.4
- unsigned int value_x;
- unsigned int value_y;
- unsigned int value_z;
- float xv;
- float yv;
- float zv;
- float angle;
- float angle2;
- void setup(){
- Wire.begin();
- Wire.beginTransmission(MPU);
- Wire.write(0x6B);
- Wire.write(0);
- Wire.endTransmission(true);
- Serial.begin(9600);
- analogReference(ADC_ref);
- }
- void loop(){
- Wire.beginTransmission(MPU);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(MPU,12,true);
- value_x=Wire.read()<<8|Wire.read();
- value_y=Wire.read()<<8|Wire.read();
- value_z=Wire.read()<<8|Wire.read();
- xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;
- yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y;
- zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;
- angle =atan2(-yv,-zv)*57.2957795+180;
- angle2 = atan2(-xv,-zv)*57.2957795+180;
- Serial.println("angle X:");
- Serial.println(angle);
- Serial.println("angle Y:");
- Serial.println(angle2);
- delay(1000);
- // GyX=Wire.read()<<8|Wire.read();
- // GyY=Wire.read()<<8|Wire.read();
- // GyZ=Wire.read()<<8|Wire.read();
- //
- // Serial.print("Accelerometer: ");
- // Serial.print("X = "); Serial.print(AcX);
- // Serial.print(" | Y = "); Serial.print(AcY);
- // Serial.print(" | Z = "); Serial.println(AcZ);
- //
- // Serial.print("Gyroscope: ");
- // Serial.print("X = "); Serial.print(GyX);
- // Serial.print(" | Y = "); Serial.print(GyY);
- // Serial.print(" | Z = "); Serial.println(GyZ);
- // Serial.println(" ");
- // delay(333);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement