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()
网友评论