#!/usr/bin/env ruby
require "socket"
require "serialport"
class Arduino
def initialize(serialport = "/dev/tty.iap", serverport = 12345)
if (not File.exist?(serialport))
raise "ERROR: Specified device does not exist."
end
@sp = SerialPort.new(serialport, 9600, 8, 1, SerialPort::NONE)
@gs = TCPServer.open(serverport)
while true
@socks = [@gs]
@addr = @gs.addr
@addr.shift
printf("server is on %s\n", @addr.join(":"))
run()
end
end
def run()
while true
nsock = select(@socks)
next if nsock == nil
for s in nsock[0]
if s == @gs
@socks.push(s.accept)
print(s, " is accepted\n")
else
if s.eof?
print(s, " is gone\n")
s.close
@socks.delete(s)
else
str = s.gets
if str.chomp == "end"
s.close
return
end
@sp.puts(str)
end
end
end
end
end
end
arduino = Arduino.new("/dev/tty.iap", 12345)
'-----------------------------
'Compiler Options
'-----------------------------
$regfile = "attiny45.dat"
$crystal = 1000000
$hwstack = 24
$swstack = 16
$framesize = 32
'-----------------------------
'Variable Definitions
'-----------------------------
Dim I As Byte
Dim Sum As Word
Dim Intpart As Integer
Dim Fracpart As Single
Dim Fracpartstr As String * 11
Dim Res As Single
'-----------------------------
'Constant Definitions
'-----------------------------
'ADC1(Pin:7)
Const Channel = 1
'Reference Voltage
Const Refvcc = 1.1
'-----------------------------
'Peripheral Settings
'-----------------------------
'PB1(Pin:6): Serial TX
Open "COMB.1:9600,8,N,1" For Output As #1
'PB3(Pin:2): LED
Config Portb.3 = Output
Config Adc = Single , Prescaler = Auto , Reference = Internal_1.1
'Config Adc = Single , Prescaler = Auto , Reference = Avcc
'ADC Ref 1.1V / Use Temperature Sensor
'Admux = &B10001111
'ADC Ref VCC / Use ADC1
'Admux = &B00000001
Start Adc
'-----------------------------
'Main Loop
'-----------------------------
Do
Sum = 0
For I = 1 To 100
Sum = Sum + Getadc(channel)
Next I
Rem (format "%01.2f" (* (/ (- (/ (* 123 4.98) 1024.0) 0.424) 6.25) 1000.0))
'Average Up
Sum = Sum / 100
'Dependent on Reference Voltage
Res = Sum * Refvcc
Res = Res / 1024.0
'Internal_1.1 Offset
'Res = Res - 0.015
Res = Res - 0.005
'Debug Print
'Print #1 , Res
'DC Offset 424mV
Res = Res - 0.424
'6.25mV/C
Res = Res / 6.25
Res = Res * 1000.0
'Formatting
Intpart = Int(res)
Fracpart = Frac(res)
Fracpartstr = Str(fracpart)
Fracpartstr = Mid(fracpartstr , 2 , 2)
Print #1 , Intpart ; Fracpartstr
'LED Flash
Gosub Ledflash
Loop
Close #1
End
'-----------------------------
'Subroutine Definitions
'-----------------------------
Ledflash:
Set Portb.3
Waitms 200
Reset Portb.3
Waitms 500
Return
$regfile = "attiny45.dat"
$crystal = 8000000
$hwstack = 24
$swstack = 16
$framesize = 32
Dim Lsb_adc As Byte 'ADC least significant byte
Dim Msb_adc As Byte 'ADC most significant byte
Dim Adc_value As Word At Lsb_adc Overlay
Dim I As Byte
Dim Summary As Integer
Dim Offset As Word
Offset = 281
Config Portb.3 = Output 'LED
Open "COMB.1:9600,8,N,1" For Output As #1 'PB1 used for serial output
Config Adc = Single , Prescaler = Auto , Reference = Internal_1.1
'ADC Ref 1.1V / Use Temperature Sensor
Admux = &B10001111
Start Adc
Do
Summary = 0
For I = 1 To 100
Set Adcsra.adsc 'Start conversion
While Adcsra.adsc = 1
'Wait conversion to complete
Wend
Lsb_adc = Adcl 'Get least significant byte
Msb_adc = Adch
Summary = Summary + Adc_value
Next I
Summary = Summary / 100 'Average
Summary = Summary - Offset
Print #1 , Summary
Set Portb.3 'LED Flash
Waitms 200
Reset Portb.3
Waitms 500
Loop
End
Recent Comments