RBoard, ESP32 動作確認用コード - gfd-dennou-club/mrubyc-esp32 GitHub Wiki

まだ微妙に違うところがある.ピン番号を変えるだけにしたい.

RBoard

#
# Sensirion SHT35
# Humidity and Temperature Sensor
#

class SHT35

  ADDR = 0x45
  
  def to_uint16( b1, b2 )
    return (b1 << 8 | b2)
  end

  def crc8(data)
    crc = 0xff
    
    data.each_byte {|b|
      crc ^= b
      8.times {
        crc <<= 1
        crc ^= 0x31  if crc > 0xff
        crc &= 0xff
      }
    }
    return crc
  end
  
  
  ##
  # SHT35 initialize
  #
  #@return [Hash]  Device identify result.
  #
  def initialize( i2c )

    @i2c = i2c
    
    @i2c.write( SHT35::ADDR, 0x30, 0xa2 )          # Reset
    res = @i2c.read( SHT35::ADDR, 3, 0xf3, 0x2d )  # Read Status
    
    if crc8(res[0,2]) != res.getbyte(2)
      puts "error"
      return false     
    end
  end
  

  ##
  # SHT35 measure
  #
  #@param  [Hash] data container
  #@return [Hash] data container
  #@return [Nil]  error.
  #
  def is_ready?
    
    @i2c.write( SHT35::ADDR, 0x2c, 0x06 )
    sleep_ms( 12 )
    res = @i2c.read( SHT35::ADDR, 6 )
    
    # check CRC
    s2 = ""
    res.each_byte {|byte|
      if s2.length == 2
        return nil  if crc8( s2 ) != byte
        s2 = ""
      else
        s2 << byte
      end
    }
    st = to_uint16( res.getbyte(0), res.getbyte(1) ).to_f
    srh = to_uint16( res.getbyte(3), res.getbyte(4) ).to_f
    
    @temp = -45 + 175 * st / 65535
    @humi = 100 * srh / 65535
    
    if @temp && @humi
      return true
    else
      return false
    end
  end
  
  def temp
    return @temp
  end
  
  def humi
    return @humi
  end
end


###
### テスト
###

#lux1 = GPIO.new(18, GPIO::IN) 
#lux2 = GPIO.new(17, GPIO::IN) 
#lux3 = GPIO.new(19, GPIO::IN)
#lux4 = GPIO.new(20, GPIO::IN)  
lux1 = ADC.new(18) 
lux2 = ADC.new(17) 
lux3 = ADC.new(19)
lux4 = ADC.new(20) 

i2c = I2C.new()
sht35 = SHT35.new( i2c )

20.times do |i|
    
    puts "----------"
    puts lux1.read
    puts lux2.read
    puts lux3.read
    puts lux4.read
    
    num = i % 2
    if sht35.is_ready?
       puts sprintf( "Temperature:%5.1f ", sht35.temp )
       puts sprintf( "Humidity:%5.1f ",    sht35.humi )
    end
    
    sleep 1
end

motor1 = GPIO.new(11, GPIO::OUT)
motor1_pwm = PWM.new(2, frequency:440, timer:3)
motor2 = GPIO.new(16, GPIO::OUT)
motor2_pwm = PWM.new(10, frequency:440, timer:3)

servo1 = PWM.new(12, frequency:50, timer:2)
servo2 = PWM.new(14, frequency:50, timer:2)

motor1_pwm.duty( 0 )
motor1.write( 1 )
motor2_pwm.duty( 0 )
motor2.write( 1 )
servo1.pulse_width_us( 1000 )
servo2.pulse_width_us( 1000 )
sleep 2 

motor1_pwm.duty( 50 )
motor1.write( 1 )
motor2_pwm.duty( 50 )
motor2.write( 1 )
servo1.pulse_width_us( 1500 )
servo2.pulse_width_us( 1500 )
sleep 2 

motor1_pwm.duty( 100 )
motor1.write( 1 )
motor2_pwm.duty( 100 )
motor2.write( 1 )
servo1.pulse_width_us( 2000 )
servo2.pulse_width_us( 2000 )
sleep 2

motor1_pwm.duty( 100 )
motor1.write( 0 )
motor2_pwm.duty( 100 )
motor2.write( 0 )
servo1.pulse_width_us( 1000 )
servo2.pulse_width_us( 1000 )
sleep 2 

motor1_pwm.duty( 50 )
motor1.write( 0 )
motor2_pwm.duty( 50 )
motor2.write( 0 )
servo1.pulse_width_us( 1500 )
servo2.pulse_width_us( 1500 )
sleep 2 

motor1_pwm.duty( 0 )
motor1.write( 0 )
motor2_pwm.duty( 0 )
motor2.write( 0 )
servo1.pulse_width_us( 2000 )
servo2.pulse_width_us( 2000 )
sleep 2 

motor1.write(1)
motor2.write(1)

loop do
  if lux1.read < 1
    motor1_pwm.duty( 100 )
  else
    motor1_pwm.duty( 0 ) 
  end
  if lux2.read < 1 
    motor2_pwm.duty( 100 )
  else
    motor2_pwm.duty( 0 )
  end
  sleep(0.1)
end

ESP32 用

lux1 = GPIO.new(36, GPIO::IN)
lux2 = GPIO.new(34, GPIO::IN)
lux3 = GPIO.new(35, GPIO::IN)
lux4 = GPIO.new(2, GPIO::IN)

i2c = I2C.new()
sht35 = SHT35.new( i2c )

servo1 = PWM.new(27, timer:2, channel:3, frequency:50)
servo2 = PWM.new(14, timer:2, channel:4, frequency:50)

motor1 = GPIO.new(25, GPIO::OUT)
motor1_pwm = PWM.new(26, frequency:440, timer:1, channel:1)
motor2 = GPIO.new(32, GPIO::OUT)
motor2_pwm = PWM.new(33, frequency:440, timer:1, channel:2)


4.times do |i|

    puts "----------"
    puts lux1.read
    puts lux2.read
    puts lux3.read
    puts lux4.read

    num = i % 2
    if sht35.is_ready?
       puts sprintf( "Temperature:%5.1f ", sht35.temp )
       puts sprintf( "Humidity:%5.1f ",    sht35.humi )
    end

    motor1_pwm.duty( 0 )
    motor1.write(num)
    motor2_pwm.duty( 0 )
    motor2.write(num)
    servo1.pulse_width_us( 1000 )
    servo2.pulse_width_us( 1000 )
    sleep 2

    motor1_pwm.duty( 40 )
    motor1.write(num)
    motor2_pwm.duty( 40 )
    motor2.write(num)
    servo1.pulse_width_us( 1500 )
    servo2.pulse_width_us( 1500 )
    sleep 2
    
    motor1_pwm.duty( 80 )
    motor1.write(num)
    motor2_pwm.duty( 80 )
    motor2.write(num)
    servo1.pulse_width_us( 2000 )
    servo2.pulse_width_us( 2000 )
    sleep 2
end

motor1.write(1)
motor2.write(1)

loop do
  if lux1.read == 0
    motor1_pwm.duty( 100 )
  else
    motor1_pwm.duty( 0 )
  end
  if lux2.read == 0
    motor2_pwm.duty( 100 )
  else
    motor2_pwm.duty( 0 )
  end
  sleep(0.1)
end
lux35 = ADC.new(35)
lux2  = ADC.new(2)
motor1 = GPIO.new(25, GPIO::OUT)
motor1_pwm = PWM.new(26, timer:1, channel:1)
motor2 = GPIO.new(32, GPIO::OUT)
motor2_pwm = PWM.new(33, timer:1, channel:2)
motor1.write(1)
motor2.write(1)

until lux35.read_raw < 100 && lux2.read_raw < 100  
  if lux35.read_raw < 100
    motor1_pwm.duty( 100 )  
  else
    motor1_pwm.duty( 0 )  
  end
  if lux2.read_raw < 100
    motor2_pwm.duty( 100 )
  else
    motor2_pwm.duty( 0 )
  end
  sleep(0.1)
end