CEB Automation Sensor Code

From Open Source Ecology
Jump to: navigation, search

Here is Arduino programming language (sketch) for the analog Hall effect sensors:

Preliminary Test

This code works with Hall effect sensor plugged into the 5V Arduino output for the left sensor pin, middle pin is at ground, and right pin is in analog input 0 (channel 14). There's a 10k pull-up resistor between the 5V and output pin.

int inPin = 0; // select the input pin for the Hall Effect Sensor int val = 0; // variable to store the value coming from the sensor

void setup() {

Serial.begin(9600); // connect to the serial port

}

void loop() {

val = analogRead(inPin);  // read the Hall Effect Sensor
Serial.println(val);
delay(200);
}

Comment: The Arduino site shows that the analog pins start at number 14. Above, teh pin number is 0, not 14. It works with 0, not 14.

Testing: move cylinder down

//begin //note - digital pins 0-13 //note - analog pins 14-19 - Huh? It's analog 0 according to the test above; stand corrected. //define initial function (called void because it creates no real output?)

void setup(){

 pinMode(0,INPUT);//corresponds to analog input 0
 pinMode(5, OUTPUT); wire 5 so it corresponds to down on the cylinder
 pinMode(6, OUTPUT); wire 6 so it corresponds to up on the cylinder
 digitalWrite(5, HIGH);

}

//define ongoing loop void loop(){} //end

Simple Test 2 - Move cylinder until it detects a sensor

//begin //note - digital pins 0-13 //note - analog pins 14-19 //define initial function (called void because it creates no real output?)

int val;

void setup(){

 pinMode(14,INPUT);//corresponds to analog input 0
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 digitalWrite(5, HIGH);
 val = analogRead(14);
 if (val < 500){
   digitalWrite(5,LOW);

}

//define ongoing loop void loop(){} //end

Working Test Code for the Main Cylinder: bouncing up and down between two sensors

Note: the logic of this code is based on this straightforward tutorial

//begin int val;

void setup(){

 pinMode(14,INPUT);//corresponds to analog input 0
 pinMode(5, OUTPUT);//corresponds to solenoid - cylinder up
 pinMode(6, OUTPUT);//corresponds to solenoid - cylinder down

}

//define ongoing loop void loop(){

 val = analogRead(14)
   if (val < 500){
   digitalWrite(5, HIGH);
   digitalWrite(6, LOW);
   }
   else{
   digitalWrite(5, LOW);
   digitialWrite(6,HIGH)
   }

}

//en