Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1.  
  2. //TASK ONE
  3. /*
  4. Blink
  5. Turns on an LED on for one second, then off for one second, repeatedly.
  6.  
  7. This example code is in the public domain.
  8. */
  9.  
  10. // Pin 13 has an LED connected on most Arduino boards.
  11. // give it a name:
  12. int led = 13;
  13.  
  14. // the setup routine runs once when you press reset:
  15. void setup() {
  16. // initialize the digital pin as an output.
  17. pinMode(led, OUTPUT);
  18. }
  19.  
  20. // the loop routine runs over and over again forever:
  21. void loop() {
  22. digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
  23. delay(1000); // wait for a second
  24. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  25. delay(1000); // wait for a second
  26. }
  27.  
  28.  
  29. /*
  30. Button
  31.  
  32. Turns on and off a light emitting diode(LED) connected to digital pin 13,
  33. when pressing a pushbutton attached to pin 2.
  34.  
  35. The circuit:
  36. - LED attached from pin 13 to ground
  37. - pushbutton attached to pin 2 from +5V
  38. - 10K resistor attached to pin 2 from ground
  39.  
  40. - Note: on most Arduinos there is already an LED on the board
  41. attached to pin 13.
  42.  
  43. created 2005
  44. by DojoDave <http://www.0j0.org>
  45. modified 30 Aug 2011
  46. by Tom Igoe
  47.  
  48. This example code is in the public domain.
  49.  
  50. http://www.arduino.cc/en/Tutorial/Button
  51. */
  52.  
  53. // constants won't change. They're used here to set pin numbers:
  54. const int buttonPin = 2; // the number of the pushbutton pin
  55. const int ledPin = 13; // the number of the LED pin
  56.  
  57. // variables will change:
  58. int buttonState = 0; // variable for reading the pushbutton status
  59.  
  60. void setup() {
  61. // initialize the LED pin as an output:
  62. pinMode(ledPin, OUTPUT);
  63. // initialize the pushbutton pin as an input:
  64. pinMode(buttonPin, INPUT);
  65. }
  66.  
  67. void loop() {
  68. // read the state of the pushbutton value:
  69. buttonState = digitalRead(buttonPin);
  70.  
  71. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  72. if (buttonState == HIGH) {
  73. // turn LED on:
  74. digitalWrite(ledPin, HIGH);
  75. } else {
  76. // turn LED off:
  77. digitalWrite(ledPin, LOW);
  78. }
  79. }
  80.  
  81.  
  82. //Controlling led by potentiometer
  83.  
  84. //Rotate the shaft of the potentiometer and you should see the luminance of the LED change.
  85. //Email:support@sunfounder.com
  86. //Website:www.sunfounder.com
  87. //2015.5.7
  88. /******************************************/
  89. const int analogPin = A0;//the analog input pin attach to analog pin A0
  90. const int ledPin = 9;//the led attach to pin 9
  91. int inputValue = 0;//variable to store the value coming from sensor
  92. int outputValue = 0;//variable to store the output value
  93. /******************************************/
  94. void setup()
  95. {
  96. }
  97. /******************************************/
  98. void loop()
  99. {
  100. inputValue = analogRead(analogPin);//read the value from the sensor
  101. outputValue = map(inputValue,0,1023,0,255);//Convert from 0-1023 proportional to the number of a number of from 0 to 255
  102. analogWrite(ledPin,outputValue);//turn the led on depend on the output value
  103. }
  104. /*******************************************/
  105.  
  106.  
  107.  
  108. //TASK TWO
  109.  
  110. /*
  111. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  112. *
  113. * by Dejan Nedelkovski,
  114. * www.HowToMechatronics.com
  115. *
  116. */
  117. // defines pins numbers
  118. const int sensorPin = 0;
  119. const int buzzerPin = 23;
  120. //const int ledPin = 9;
  121.  
  122. // We'll also set up some global variables for the light level:
  123.  
  124. int lightLevel, high = 0, low = 1023;
  125. const int trigPin = 9;
  126. const int echoPin = 10;
  127. int motorPin = 3;
  128. // defines variables
  129. long duration;
  130. int motorspeed;
  131. int distance;
  132. void setup() {
  133. //pinMode(ledPin, OUTPUT);
  134. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  135. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  136. Serial.begin(9600); // Starts the serial communication
  137. pinMode(motorPin, OUTPUT);
  138. pinMode(buzzerPin,OUTPUT);
  139. while (! Serial);
  140. Serial.println("Speed 0 to 255");
  141. }
  142. void loop() {
  143. // Clears the trigPin
  144. digitalWrite(trigPin, LOW);
  145. delayMicroseconds(2);
  146. // Sets the trigPin on HIGH state for 10 micro seconds
  147. digitalWrite(trigPin, HIGH);
  148. delayMicroseconds(10);
  149. digitalWrite(trigPin, LOW);
  150. // Reads the echoPin, returns the sound wave travel time in microseconds
  151. duration = pulseIn(echoPin, HIGH);
  152. // Calculating the distance
  153. distance= duration*0.034/2;
  154.  
  155. if (distance > 25) {
  156. distance = 25;
  157. }
  158. if(distance > 1000) {
  159. distance = 0;
  160. }
  161.  
  162.  
  163.  
  164. int speed = distance*10;
  165. if (speed >= 0 && speed <= 255)
  166. {
  167. analogWrite(motorPin, speed);
  168. Serial.println("speed");
  169. Serial.println(speed);
  170. }
  171.  
  172.  
  173.  
  174. // Prints the distance on the Serial Monitor
  175. delay(1000);
  176.  
  177.  
  178. lightLevel = analogRead(sensorPin);
  179.  
  180.  
  181. //delay(1000);
  182.  
  183. manualTune(); // manually change the range from light to dark
  184.  
  185. if(lightLevel < 100) {
  186. //tone(buzzerPin,HIGH);
  187. //delay(1000);
  188. digitalWrite(buzzerPin,HIGH);
  189. //delay(1000);
  190.  
  191. }
  192. else {
  193. //tone(buzzerPin,500);
  194. //delay(1000);
  195.  
  196. digitalWrite(buzzerPin,LOW);
  197. //delay(1000);
  198.  
  199.  
  200. }
  201.  
  202.  
  203. //analogWrite(ledPin, lightLevel);
  204. Serial.println("lightLevel");
  205. Serial.println(lightLevel);
  206.  
  207. //Serial.print("Distance: ");
  208. //Serial.println(distance);
  209. }
  210.  
  211.  
  212. void manualTune()
  213. {
  214.  
  215. lightLevel = map(lightLevel, 0, 1023, 0, 255);
  216. lightLevel = constrain(lightLevel, 0, 255);
  217.  
  218.  
  219. }
  220.  
  221.  
  222. void autoTune()
  223. {
  224.  
  225.  
  226. if (lightLevel < low)
  227. {
  228. low = lightLevel;
  229. }
  230.  
  231. if (lightLevel > high)
  232. {
  233. high = lightLevel;
  234. }
  235.  
  236.  
  237. lightLevel = map(lightLevel, low+30, high-30, 0, 255);
  238. lightLevel = constrain(lightLevel, 0, 255);
  239.  
  240.  
  241. }
  242.  
  243.  
  244.  
  245. //TASK THREE
  246.  
  247. #include<Wire.h>
  248. const int MPU=0x68;
  249. int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
  250. #define ADC_ref 5
  251. #define zero_x 1.799
  252. #define zero_y 1.799
  253. #define zero_z 1.799
  254. #define sensitivity_x 0.4
  255. #define sensitivity_y 0.4
  256. #define sensitivity_z 0.4
  257.  
  258.  
  259. unsigned int value_x;
  260. unsigned int value_y;
  261. unsigned int value_z;
  262.  
  263. float xv;
  264. float yv;
  265. float zv;
  266.  
  267. float angle;
  268. float angle2;
  269.  
  270.  
  271.  
  272. void setup(){
  273. Wire.begin();
  274. Wire.beginTransmission(MPU);
  275. Wire.write(0x6B);
  276. Wire.write(0);
  277. Wire.endTransmission(true);
  278. Serial.begin(9600);
  279. analogReference(ADC_ref);
  280.  
  281. }
  282. void loop(){
  283. Wire.beginTransmission(MPU);
  284. Wire.write(0x3B);
  285. Wire.endTransmission(false);
  286. Wire.requestFrom(MPU,12,true);
  287. value_x=Wire.read()<<8|Wire.read();
  288. value_y=Wire.read()<<8|Wire.read();
  289. value_z=Wire.read()<<8|Wire.read();
  290.  
  291. xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;
  292. yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y;
  293. zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;
  294. angle =atan2(-yv,-zv)*57.2957795+180;
  295. angle2 = atan2(-xv,-zv)*57.2957795+180;
  296.  
  297. Serial.println("angle X:");
  298. Serial.println(angle);
  299. Serial.println("angle Y:");
  300.  
  301. Serial.println(angle2);
  302. delay(1000);
  303.  
  304.  
  305. // GyX=Wire.read()<<8|Wire.read();
  306. // GyY=Wire.read()<<8|Wire.read();
  307. // GyZ=Wire.read()<<8|Wire.read();
  308. //
  309. // Serial.print("Accelerometer: ");
  310. // Serial.print("X = "); Serial.print(AcX);
  311. // Serial.print(" | Y = "); Serial.print(AcY);
  312. // Serial.print(" | Z = "); Serial.println(AcZ);
  313. //
  314. // Serial.print("Gyroscope: ");
  315. // Serial.print("X = "); Serial.print(GyX);
  316. // Serial.print(" | Y = "); Serial.print(GyY);
  317. // Serial.print(" | Z = "); Serial.println(GyZ);
  318. // Serial.println(" ");
  319. // delay(333);
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement