-ไปหน้ามอไปที่ร้านขายของอิเล็กทรอนิกส์ ได้ไปเจอ Colo sensor ราคา250บาท พอดีเลยอยากทำเครื่องวัดค่าระดับสี
-สำหรับวันนี้ได้เขียนโค้ด Encoder โดยใช้ Interrupt

http://playground.arduino.cc/Main/RotaryEncoders
ตัวอย่างที่นำมาอ่าน
ปัญหาที่เกิดขึ้นคือ interrupt ใช้แล้วค่ามันลบไม่เหลือ 0 แล้วค่าไม่ติดลบ ตัวอย่าง 120 พอลบแล้วมันจะได้119 แต่แก้ได้คือให้ void loop() ให้ค่ารอบเป็น0
ตัวอย่างโค้ดที่เอามาดัดแปลง
/* read a rotary encoder with interrupts
Encoder hooked up with common to GROUND,
encoder0PinA to pin 2, encoder0PinB to pin 4 (or pin 3 see below)
it doesn't matter which encoder pin you use for A or B
uses Arduino pullups on A & B channel outputs
turning on the pullups saves having to hook up resistors
to the A & B channel outputs
*/
#define encoder0PinA 2
#define encoder0PinB 4
volatile unsigned int encoder0Pos = 0;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}
void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning
* forward. If they're different, it's going backward.
*
* For more information on speeding up this process, see
* [Reference/PortManipulation], specifically the PIND register.
*/
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}
Serial.println (encoder0Pos, DEC);
}
/* See this expanded function to get a better understanding of the
* meanings of the four possible (pinA, pinB) value pairs:
*/
void doEncoder_Expanded(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
// you don't want serial slowing down your program if not needed
}
/* to read the other two transitions - just use another attachInterrupt()
in the setup and duplicate the doEncoder function into say,
doEncoderA and doEncoderB.
You also need to move the other encoder wire over to pin 3 (interrupt 1).
*/
Encoder hooked up with common to GROUND,
encoder0PinA to pin 2, encoder0PinB to pin 4 (or pin 3 see below)
it doesn't matter which encoder pin you use for A or B
uses Arduino pullups on A & B channel outputs
turning on the pullups saves having to hook up resistors
to the A & B channel outputs
*/
#define encoder0PinA 2
#define encoder0PinB 4
volatile unsigned int encoder0Pos = 0;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}
void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning
* forward. If they're different, it's going backward.
*
* For more information on speeding up this process, see
* [Reference/PortManipulation], specifically the PIND register.
*/
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}
Serial.println (encoder0Pos, DEC);
}
/* See this expanded function to get a better understanding of the
* meanings of the four possible (pinA, pinB) value pairs:
*/
void doEncoder_Expanded(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
// you don't want serial slowing down your program if not needed
}
/* to read the other two transitions - just use another attachInterrupt()
in the setup and duplicate the doEncoder function into say,
doEncoderA and doEncoderB.
You also need to move the other encoder wire over to pin 3 (interrupt 1).
*/
ส่วนโค้ดที่แก้เสร็จแล้ว และได้ทำการเขียนเรียบร้อย
int A = 2;
int B = 3;
int x = 0;
void setup() {
pinMode(A,INPUT_PULLUP);
pinMode(B,INPUT_PULLUP);
attachInterrupt(0, ENA, HIGH);
Serial.begin(9600);
Serial.println("start");
}
void loop() {
x=0;
delay(100);
}
void ENA() {
if (digitalRead(A) == HIGH) { // found a low-to-high on channel A
if (digitalRead(B) == HIGH) { // check channel B to see which way
// encoder is turning
x = x + 1; // CCW
}
if (digitalRead(B) == LOW) { // check channel B to see which way
// encoder is turning
x = x - 1; // CCW
}
}
Serial.println (x, DEC);
delay(100);
}
หัวฉีด3D printer และรูปตัวอย่าง
https://www.google.co.th/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=%E0%B8%82%E0%B8%B2%E0%B8%A2+%E0%B8%AB%E0%B8%B1%E0%B8%A7%E0%B8%89%E0%B8%B5%E0%B8%94+3d+printer&spell=1
ส่วนราคาต้องเว็ปด้านล่างต่อไปนี้ ส่วนราคาในเว็ป 1750บาท
http://www.filastruder.com/products/all-metal-e3d-v6-hotend
คริปปากกา3D และวิธีการใช้
https://www.youtube.com/watch?v=W1-_XNoxZs0
สุดท้ายว่างโครงการ ไปเปิดหูเปิดตาที่กรุงเทพ


ไม่มีความคิดเห็น:
แสดงความคิดเห็น