美文网首页
【树莓派+arduino实验记录11 】电位传感器

【树莓派+arduino实验记录11 】电位传感器

作者: Geekero | 来源:发表于2020-08-21 09:33 被阅读0次

Arduino

const int analogPin = A0; //the analog input pin attach to
const int ledPin = 13;
int inputValue = 0; //variable to store the value coming from sensor
/***************************************************/
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
/***************************************************/
void loop()
{
  inputValue = analogRead(analogPin); //read the value from the sensor
  Serial.println(inputValue);
  digitalWrite(ledPin, LOW);
  delay(inputValue);
  digitalWrite(ledPin, HIGH);
  delay(inputValue);
}
/************************/

树莓派

C

#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>

#define PCF    120

int main(void)
{
    int value;
    wiringPiSetup();
    //Setup pcf8591 on base pin 120, and address 0x48
    pcf8591Setup(PCF, 0x48);
    while(1) //loop forever
    {
        value = analogRead(PCF + 0);
        printf("Value: %d\n", value);
        analogWrite(PCF + 0, value);
        delay(200);
    }
    return 0;
}

Python

#!/usr/bin/env python
import PCF8591 as ADC
import time

def setup():
    ADC.setup(0x48)

def loop():
#    status = 1
    while True:
        print('Value:', ADC.read(0))
        Value = ADC.read(0)
        outvalue = map(Value, 0, 255, 120, 255)
        ADC.write(outvalue)
    time.sleep(0.2)

def destroy():
    ADC.write(0)

def map(x, in_min, in_max, out_min, out_max):
   '''To map the value from range to another'''
   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()

网友评论

      本文标题:【树莓派+arduino实验记录11 】电位传感器

      本文链接:https://www.haomeiwen.com/subject/yrcejktx.html