Advertisement
Guest User

Suntracking Arduino scatch

a guest
Mar 27th, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1.  /*
  2.  Arduino Sun Tracker
  3.  Demonstrates analog input by reading analog sensors on analog pin 0 to 3 and
  4.  turning on and off a PWM signal on digital pin 10 and 11.
  5.  The amount of time the PWM signal will be switched on depends on
  6.  the value obtained by delay() function.
  7.  
  8.  * Note: because most Arduinos have a built-in LED attached
  9.  to pin 13 on the board, the LED is optional.
  10.  
  11.  Created by Colin
  12.  Modified 29.03.2014
  13.  
  14.  This code is for public use.
  15.  */
  16.  
  17. int sensorPin = A0;           // select the input pins for the analog sensors
  18. int sensorPin1 = A1;
  19. int sensorPin2 = A2;
  20. int sensorPin3 = A3;
  21. //int rainsensor = A4;
  22. int joyPinx = A5;                 // connecetd to VRx
  23. int joyPiny = A6;                 // connecetd to VRy
  24. float vrx = 0;                  // variable to store the value from VRx
  25. float vry = 0;                  // variable to store the value from VRy
  26.  
  27. //const int limitew = 2;        // the number of the EW limit switch pin
  28. //const int limitew1 = 3;
  29. //const int limitns = 6;        // the number of the NS limit switch pin
  30. //const int limitns1 = 7;
  31.  
  32. // const int arrestorns = 4;    // output Pin for arrestor solenoid
  33. // const int arrestorew = 5;    // output Pin for arrestor solenoid
  34.  
  35. // const int swew = 0;           // Manual movement switches
  36. // const int swew1 = 1;
  37. // const int swns = 8;
  38. // const int swns1 = 9;
  39.  
  40. int output = 10;            // select the PWM pin for the Allegro Stepper Driver
  41. int output1 = 11;
  42. int outdir = 12;            // select the pin for the direction
  43. int outdir1 = 13;
  44.  
  45. float sensorValue = 0;        // variable to store the value coming from the sensor
  46. float sensorValue1 = 0;
  47. float sensorValue2 = 0;
  48. float sensorValue3 = 0;
  49. //float rainvalue = 0;
  50.  
  51. //int limitsw = 0;        // variable to store the value coming from the inputs
  52. //int limitsw1 = 0;
  53. //int limitsw2 = 0;      
  54. //int limitsw3 = 0;
  55.  
  56. //int buttonew = 0;
  57. //int buttonew1 = 0;
  58. //int buttonns = 0;
  59. //int buttonns1 = 0;
  60.  
  61. // int lockns = 0;
  62. // int lockew = 0;
  63.  
  64. void setup() {
  65.  
  66.   // initialize serial communication at 9600 bits per second:
  67.   Serial.begin(9600);
  68.  
  69.   // declare the INPUTS:
  70. //  pinMode(limitew, INPUT);
  71. //  pinMode(limitew1, INPUT);
  72. //  pinMode(limitns, INPUT);
  73. //  pinMode(limitns1, INPUT);
  74. //  pinMode(swew, INPUT);
  75. //  pinMode(swew1, INPUT);
  76. //  pinMode(swns, INPUT);
  77. //  pinMode(swns1, INPUT);
  78.  
  79.   // declare the output PIN as an OUTPUT:
  80.   pinMode(output,  OUTPUT);
  81.   pinMode(output1, OUTPUT);
  82.   pinMode(outdir, OUTPUT);
  83.   pinMode(outdir1, OUTPUT);
  84. //pinMode(arrestorns, OUTPUT);
  85. //pinMode(arrestorew, OUTPUT);
  86. }
  87.  
  88. void loop() {
  89.   // read the analog values from the sensors:
  90.   sensorValue  = analogRead(sensorPin) *(5.0 / 1023.0);    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  91.   sensorValue1 = analogRead(sensorPin1)*(5.0 / 1023.0);
  92.   sensorValue2 = analogRead(sensorPin2)*(5.0 / 1023.0);
  93.   sensorValue3 = analogRead(sensorPin3)*(5.0 / 1023.0);
  94. //  rainvalue = analogRead(rainsensor)*(5.0 / 1023.0);
  95.   vrx = analogRead(joyPinx)*(5.0 / 1023.0);
  96.   vry = analogRead(joyPiny)*(5.0 / 1023.0);
  97.  
  98. //  limitsw  = digitalRead(limitew);                       // Read signal from input Pins and write to variable
  99. //  limitsw1  = digitalRead(limitew1);
  100. //  limitsw2  = digitalRead(limitns);
  101. //  limitsw3  = digitalRead(limitns1);
  102.  
  103. //  buttonew = digitalRead(swew);
  104. //  buttonew1 = digitalRead(swew1);
  105. //  buttonns = digitalRead(swns);
  106. //  buttonns1 = digitalRead(swns1);
  107.  
  108. //lockns = digitalRead(arrestorns);
  109. //lockew = digitalRead(arrestorew);
  110.  
  111.   //Calculating average values from light sensors. Result used for day night detection
  112.   float avns = (sensorValue + sensorValue1) / 2;     // average value NS          
  113.   float avew = (sensorValue2 + sensorValue3) / 2;    // average value EW
  114.  
  115.   Serial.println("Voltage LDR");
  116.   Serial.println(sensorValue);
  117.   Serial.println(sensorValue1);
  118.   Serial.println(sensorValue2);
  119.   Serial.println(sensorValue3);
  120.  
  121. /*  Serial.println("Is it raining?");
  122.   Serial.println(rainvalue);
  123.  
  124.   Serial.println("Value of limitswitches");
  125.   Serial.println(limitsw);
  126.   Serial.println("Value of limitsw1");
  127.   Serial.println(limitsw1);
  128.   Serial.println("Value of limitsw2");
  129.   Serial.println(limitsw2);
  130.   Serial.println("Value of limitsw3");
  131.   Serial.println(limitsw3);
  132.  
  133.   Serial.println("Value of Output1");
  134.   Serial.println(output);
  135.   Serial.println("Value of Ouput2");
  136.   Serial.println(output1);*/
  137.  
  138.   Serial.println("Average NS");
  139.   Serial.println(avns);
  140.   Serial.println("Average EW");
  141.   Serial.println(avew);
  142.  
  143.  
  144.  
  145.  
  146. /*if ((avew < 0.3) && (humidityValue > 4.0) && (!buttonew || !buttonew1 || !buttonns || !buttonns1) && (!limitsw && !limit2))
  147. {
  148. digitalWrite(arrestorns, HIGH);  
  149. digitalWrite(arrestorew, HIGH);  
  150. }
  151. */
  152. /*if ((avew < 0.3) && (humidityValue > 4.0) && (limitsw && limit2))
  153. {
  154. digitalWrite(arrestorns, LOW);  
  155. digitalWrite(arrestorew, LOW);  
  156. }
  157. */
  158. /*if ((avew > 0.3) && (humidityValue < 4.0) && limitsw && limit2)
  159. {
  160. digitalWrite(arrestorns, HIGH);  
  161. digitalWrite(arrestorew, HIGH);  
  162. }
  163. */
  164. /*if ((avew > 0.3) && (humidityValue < 4.0) && !limitsw && !limit2)
  165. {
  166. digitalWrite(arrestorns, LOW);  
  167. digitalWrite(arrestorew, LOW);  
  168. }
  169. */
  170.  
  171. if /*(((*/((sensorValue > (sensorValue1 + 0.01 )) && (avew > 0.8)) /*&& (rainvalue < 4.0) && !limitsw) || (vrx<2 && !limitsw)) || (((avew < 0.8) || (rainvalue > 4.0)) && !limitsw)) && (!vrx>3 || !vry<2 || !vry>3))*/
  172. {
  173. digitalWrite(outdir, LOW);
  174. analogWrite(output, 128);
  175. delay(20);
  176. analogWrite(output, LOW);
  177. //digitalWrite(outdir, LOW);
  178. }
  179. else if /*((*/((sensorValue < (sensorValue1 - 0.01)) && (avew > 0.8)) /*&& (rainvalue < 4.0) && !limitsw1) || (vrx>3 && !limitsw1)) && (!vrx<2 || !vry<2 || !vry>2))*/
  180. {
  181. digitalWrite(outdir, HIGH);
  182. analogWrite(output, 128);
  183. delay(20);
  184. analogWrite(output, LOW);
  185. //digitalWrite(outdir, HIGH);
  186. }
  187.  
  188. if /*(((*/((sensorValue2 > (sensorValue3 + 0.01)) && (avew > 0.8))/* && (rainvalue < 4.0) && !limitsw2) || (vry<2 && !limitsw2)) || (((avew < 0.8) || (rainvalue > 4.0)) && !limitsw2)) && (!vrx<2 || !vrx>3 || !vry>3))*/
  189. {
  190. digitalWrite(outdir1, LOW);
  191. analogWrite(output1, 128);
  192. delay(20);
  193. analogWrite(output1, LOW);
  194. //digitalWrite(outdir1, LOW);
  195. }
  196. else if /*((*/((sensorValue2 < (sensorValue3 - 0.01)) && (avew > 0.8))/* && (rainvalue < 4.0) && !limitsw3) || (vry>3 && !limitsw3)) && (!vrx<2 || !vrx>3 || !vry<2))*/
  197. {
  198. digitalWrite(outdir1, HIGH);  
  199. analogWrite(output1, 128);
  200. delay(20);
  201. analogWrite(output1, LOW);
  202. //digitalWrite(outdir1, HIGH);
  203. }
  204.  
  205. delay(500);
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement