จาก code นนี้ใครพอจะช่วยตัด code ออกให้หน่อยได้มั่ยคับ พอดีผมอยากจะให้มันแสดงตอน มอนิเตอร์ อย่างเดียวอะคับ ผมไม่รู้จะลบตรงไหน
คืออยากได้สูตรมันอะคับ ตอนนี้หาข้อมูลการใช้โมดุล ACS712-5A อียู่อะหาสูตรไม่ได้อะคับอยากจะวัดกระแส หาจากgoogle เจอ code อะไรก็วัดไม่ได้
ซักที ใครพอช่วยเอาสูตรจาก code นี้ แล้วมาโชว์ มอนิเตอร์ให้หน่อยได้มั่ยคับ TT
ที่มา ::
http://cpre.kmutnb.ac.th/esl/learning/index.php?article=acs71x-current-sensor&start=60
code ----
/////////////////////////////////////////////////////////////////////////
// Author: RSP @ ESL (Embedded System Lab), KMUTNB
// Date: 2014-MAR-26
// Target Board: Arduino (ATmega328P, 5V, 16MHz)
// Arduino IDE: version 1.0.5
// Description: This Arduino Sketch is used to measure the analog voltage
// from an ACS712/ACS714 current sensor module. It also shows the sensor
// value on a 16x2 LCD module (connected via an I2C converter module).
//////////////////////////////////////////////////////////////////////////
#include <Wire.h> // use the Wire library
// See:
http://arduino.cc/en/reference/wire
// Arduino Uno: SDA = A4, SCL = A5
// PCF8574 I2C I/O Expander
#define ADDR_BITS (0B110) // A0=1,A1=1,A2=1 (pull-up, default)
#define I2C_SLAVE_ADDR ((0B0100000) | ADDR_BITS) // 7-bit address
#define NUM_LINES (2)
#define LINE_WIDTH (16)
// LCD commands (instructions)
#define CMD_CLEAR_DISPLAY 0x01
#define CMD_RETURN_HOME 0x02
#define CMD_ENTRY_MODE_SET 0x04
#define CMD_DISPLAY_CTRL 0x08
#define CMD_CURSOR_DISPLAY_SHIFT 0x10
#define CMD_FUNCTION_SET 0x20
#define CMD_SET_CGRAM_ADDR 0x40
#define CMD_SET_DDRAM_ADDR 0x80
// function set
#define USE_2LINE 0x08
// entry mode set
#define ENTRY_LEFT 0x02
// display control
#define BLINK_ON 0x01
#define CURSOR_ON 0x02
#define DISPLAY_ON 0x04
// display/cursor shift control
#define CURSOR_MOVE 0x04
#define DISPLAY_SHIFT 0x08
#define RS_BIT (1 << 0)
#define RW_BIT (1 << 1)
#define EN_BIT (1 << 2)
uint8_t ctrl_bits = 0x00;
void write_4bits( uint8_t data ) {
data <<= 4;
data |= (1<<3); // turn on backlight
ctrl_bits &= ~EN_BIT; // EN = 0
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits );
Wire.endTransmission();
ctrl_bits |= EN_BIT; // EN = 1
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits ); // send Enable pulse
Wire.endTransmission();
ctrl_bits &= ~EN_BIT; // EN = 0
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits );
Wire.endTransmission();
}
void lcd_reset() {
// start with the 8-bit interface
write_4bits( 0x03 ); delay(5);
write_4bits( 0x03 ); delay(5);
write_4bits( 0x03 ); delay(1);
write_4bits( 0x02 ); // switch to 4-bit interface
}
void lcd_send_cmd( uint8_t value ) {
ctrl_bits &= ~RS_BIT; // RS = 0
write_4bits( value >> 4 ); // high nibble
write_4bits( value & 0x0f ); // low nibble
delayMicroseconds( 50 );
}
void lcd_send_data( uint8_t value ) {
ctrl_bits |= RS_BIT; // RS = 1
write_4bits( value >> 4 ); // high nibble
write_4bits( value & 0x0f ); // low nibble
}
void lcd_clear_display() {
lcd_send_cmd( CMD_CLEAR_DISPLAY ); delay(2);
}
void lcd_set_cursor( uint8_t col, uint8_t row ) {
static uint8_t offsets[] = {0x00, 0x40, 0x14, 0x54} ;
lcd_send_cmd( CMD_SET_DDRAM_ADDR | (col + offsets[row]) );
}
void lcd_init() {
delay(50);
lcd_reset();
lcd_send_cmd( CMD_FUNCTION_SET | USE_2LINE );
lcd_send_cmd( CMD_DISPLAY_CTRL | DISPLAY_ON );
lcd_send_cmd( CMD_ENTRY_MODE_SET | ENTRY_LEFT );
lcd_send_cmd( CMD_RETURN_HOME );
delay(2);
}
void update_display( uint8_t line, const char*text ) {
lcd_set_cursor( 0, line ); // goto the line begin
for ( uint8_t i=0, len=strlen(text); i < len; i++ ) {
lcd_send_data( text
);
}
}
//////////////////////////////////////////////////////////////////////////
char buf[20];
#define ANALOG_PIN (A0)
#define VCC (5000L) /* mV (May need to adjust this value!) */
#define OUTPUT_SENSITIVITY (185.0f) /* mV/A */
int16_t centered_value;
void setup() {
Wire.begin();
TWBR = 12; // for 400kHz
lcd_init();
delay(100);
centered_value = 0;
for (int i=0; i < 8; i++) {
centered_value += analogRead( ANALOG_PIN );
delay(5);
}
centered_value /= 8; // average value
}
void loop() {
int16_t value = analogRead( ANALOG_PIN );
int32_t milli_volt = (VCC*(value - centered_value)/1024);
int16_t mv = (int16_t) milli_volt;
float current = abs(mv) / OUTPUT_SENSITIVITY;
int16_t x = (int16_t)(current);
int16_t y = (int16_t)(100*current);
char pm = (mv < 0) ? '-' : '+';
sprintf( buf,"%d %4d %c%d.dA", centered_value, value, pm, x, (y0) );
lcd_clear_display();
update_display( 0, "ACS712 (5A)" );
update_display( 1, buf );
delay(100);
}
//////////////////////////////////////////////////////////////////////////
ขอความช่วยเหลือดู code ให้หน่อยคับ Arduino
คืออยากได้สูตรมันอะคับ ตอนนี้หาข้อมูลการใช้โมดุล ACS712-5A อียู่อะหาสูตรไม่ได้อะคับอยากจะวัดกระแส หาจากgoogle เจอ code อะไรก็วัดไม่ได้
ซักที ใครพอช่วยเอาสูตรจาก code นี้ แล้วมาโชว์ มอนิเตอร์ให้หน่อยได้มั่ยคับ TT
ที่มา :: http://cpre.kmutnb.ac.th/esl/learning/index.php?article=acs71x-current-sensor&start=60
code ----
/////////////////////////////////////////////////////////////////////////
// Author: RSP @ ESL (Embedded System Lab), KMUTNB
// Date: 2014-MAR-26
// Target Board: Arduino (ATmega328P, 5V, 16MHz)
// Arduino IDE: version 1.0.5
// Description: This Arduino Sketch is used to measure the analog voltage
// from an ACS712/ACS714 current sensor module. It also shows the sensor
// value on a 16x2 LCD module (connected via an I2C converter module).
//////////////////////////////////////////////////////////////////////////
#include <Wire.h> // use the Wire library
// See: http://arduino.cc/en/reference/wire
// Arduino Uno: SDA = A4, SCL = A5
// PCF8574 I2C I/O Expander
#define ADDR_BITS (0B110) // A0=1,A1=1,A2=1 (pull-up, default)
#define I2C_SLAVE_ADDR ((0B0100000) | ADDR_BITS) // 7-bit address
#define NUM_LINES (2)
#define LINE_WIDTH (16)
// LCD commands (instructions)
#define CMD_CLEAR_DISPLAY 0x01
#define CMD_RETURN_HOME 0x02
#define CMD_ENTRY_MODE_SET 0x04
#define CMD_DISPLAY_CTRL 0x08
#define CMD_CURSOR_DISPLAY_SHIFT 0x10
#define CMD_FUNCTION_SET 0x20
#define CMD_SET_CGRAM_ADDR 0x40
#define CMD_SET_DDRAM_ADDR 0x80
// function set
#define USE_2LINE 0x08
// entry mode set
#define ENTRY_LEFT 0x02
// display control
#define BLINK_ON 0x01
#define CURSOR_ON 0x02
#define DISPLAY_ON 0x04
// display/cursor shift control
#define CURSOR_MOVE 0x04
#define DISPLAY_SHIFT 0x08
#define RS_BIT (1 << 0)
#define RW_BIT (1 << 1)
#define EN_BIT (1 << 2)
uint8_t ctrl_bits = 0x00;
void write_4bits( uint8_t data ) {
data <<= 4;
data |= (1<<3); // turn on backlight
ctrl_bits &= ~EN_BIT; // EN = 0
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits );
Wire.endTransmission();
ctrl_bits |= EN_BIT; // EN = 1
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits ); // send Enable pulse
Wire.endTransmission();
ctrl_bits &= ~EN_BIT; // EN = 0
Wire.beginTransmission( I2C_SLAVE_ADDR );
Wire.write( data | ctrl_bits );
Wire.endTransmission();
}
void lcd_reset() {
// start with the 8-bit interface
write_4bits( 0x03 ); delay(5);
write_4bits( 0x03 ); delay(5);
write_4bits( 0x03 ); delay(1);
write_4bits( 0x02 ); // switch to 4-bit interface
}
void lcd_send_cmd( uint8_t value ) {
ctrl_bits &= ~RS_BIT; // RS = 0
write_4bits( value >> 4 ); // high nibble
write_4bits( value & 0x0f ); // low nibble
delayMicroseconds( 50 );
}
void lcd_send_data( uint8_t value ) {
ctrl_bits |= RS_BIT; // RS = 1
write_4bits( value >> 4 ); // high nibble
write_4bits( value & 0x0f ); // low nibble
}
void lcd_clear_display() {
lcd_send_cmd( CMD_CLEAR_DISPLAY ); delay(2);
}
void lcd_set_cursor( uint8_t col, uint8_t row ) {
static uint8_t offsets[] = {0x00, 0x40, 0x14, 0x54} ;
lcd_send_cmd( CMD_SET_DDRAM_ADDR | (col + offsets[row]) );
}
void lcd_init() {
delay(50);
lcd_reset();
lcd_send_cmd( CMD_FUNCTION_SET | USE_2LINE );
lcd_send_cmd( CMD_DISPLAY_CTRL | DISPLAY_ON );
lcd_send_cmd( CMD_ENTRY_MODE_SET | ENTRY_LEFT );
lcd_send_cmd( CMD_RETURN_HOME );
delay(2);
}
void update_display( uint8_t line, const char*text ) {
lcd_set_cursor( 0, line ); // goto the line begin
for ( uint8_t i=0, len=strlen(text); i < len; i++ ) {
lcd_send_data( text );
}
}
//////////////////////////////////////////////////////////////////////////
char buf[20];
#define ANALOG_PIN (A0)
#define VCC (5000L) /* mV (May need to adjust this value!) */
#define OUTPUT_SENSITIVITY (185.0f) /* mV/A */
int16_t centered_value;
void setup() {
Wire.begin();
TWBR = 12; // for 400kHz
lcd_init();
delay(100);
centered_value = 0;
for (int i=0; i < 8; i++) {
centered_value += analogRead( ANALOG_PIN );
delay(5);
}
centered_value /= 8; // average value
}
void loop() {
int16_t value = analogRead( ANALOG_PIN );
int32_t milli_volt = (VCC*(value - centered_value)/1024);
int16_t mv = (int16_t) milli_volt;
float current = abs(mv) / OUTPUT_SENSITIVITY;
int16_t x = (int16_t)(current);
int16_t y = (int16_t)(100*current);
char pm = (mv < 0) ? '-' : '+';
sprintf( buf,"%d %4d %c%d.dA", centered_value, value, pm, x, (y0) );
lcd_clear_display();
update_display( 0, "ACS712 (5A)" );
update_display( 1, buf );
delay(100);
}
//////////////////////////////////////////////////////////////////////////