CEB Automation Sensor Code: Difference between revisions
(Created page with 'Here is Arduino programming language (sketch) for the analog Hall effect sensors: =Simple Test 1= //begin //note - digital pins 0-13 //note - analog pins 14-19 //define initial…') |
mNo edit summary |
||
(5 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
Here is Arduino programming language (sketch) for the analog Hall effect sensors: | Here is Arduino programming language (sketch) for the analog Hall effect sensors: | ||
=Simple Test | =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 | //begin | ||
//note - digital pins 0-13 | //note - digital pins 0-13 | ||
//note - analog pins 14-19 | //note - analog pins 14-19 | ||
//define initial function (void because it creates no output) | //define initial function (called void because it creates no real output?) | ||
int val; | |||
void setup(){ | void setup(){ | ||
Line 13: | Line 51: | ||
pinMode(6, OUTPUT); | pinMode(6, OUTPUT); | ||
digitalWrite(5, HIGH); | digitalWrite(5, HIGH); | ||
val = analogRead(14); | |||
if (val < 500){ | |||
digitalWrite(5,LOW); | |||
} | } | ||
Line 20: | Line 61: | ||
=Working Test Code for the Main Cylinder: bouncing up and down between two sensors= | =Working Test Code for the Main Cylinder: bouncing up and down between two sensors= | ||
Note: the logic of this code is based on this [http://www.ladyada.net/learn/arduino/lesson5.html straightforward tutorial] | |||
//begin | //begin | ||
int val; | |||
void setup(){ | void setup(){ | ||
Line 31: | Line 75: | ||
//define ongoing loop | //define ongoing loop | ||
void loop(){ | void loop(){ | ||
digitalWrite(5, HIGH); | |||
val = analogRead(14) | |||
if (val < 500){ | |||
digitalWrite(5, HIGH); | |||
digitalWrite(6, LOW); | |||
} | |||
else{ | |||
digitalWrite(5, LOW); | |||
digitialWrite(6,HIGH) | |||
} | |||
} | } | ||
//en | //en | ||
[[Category:CAD]] | |||
[[Category:Digital Fabrication]] |
Latest revision as of 12:10, 4 January 2011
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