Home
- Uppgifter
- Skriven av Krister Karlsson
- Kategori: News
- Träffar: 7496
This article shows two code samples.
The first shows the code run by an Trinket acting as a volt meter.
The measured value is keyed out on the USB port of the trinket to a host.
Before printing the value to USB the reading is multiplied with a factor so that actual voltage is represented in clear text.
The Pyton script in the end of the article is suitable to run on a Raspberry Pi in order to receive the keyed values and store them in a file named volt
Video shows the Trinket measuring voltage and keying the result to Windows emulating a USB keyboard. The two resistors divides the input voltage as the measured voltage is higher than the trinket can handle.
#include <Keyboard.h>
/*
TrinketKeyboard example
For Trinket by Adafruit Industries
*/
#include <TrinketKeyboard.h>
#include <math.h>
#define PIN_BUTTON_CAPITAL_A 0
#define PIN_BUTTON_STRING 2
int sensorPin = 1; // select the input pin for the potentiometer
int sensorValue =0;
int led = 1;
int x = 0;
int y = 0;
long z = 0;
int w = 0;
long v = 0;
long l = 0;
void setup()
{
// button pins as inputs
pinMode(PIN_BUTTON_CAPITAL_A, INPUT);
pinMode(PIN_BUTTON_STRING, INPUT);
// setting input pins to high means turning on internal pull-up resistors
digitalWrite(PIN_BUTTON_CAPITAL_A, HIGH);
digitalWrite(PIN_BUTTON_STRING, HIGH);
pinMode(2,INPUT);
// remember, the buttons are active-low, they read LOW when they are not pressed
// start USB stuff
TrinketKeyboard.begin();
}
void loop()
{
// the poll function must be called at least once every 10 ms
// or cause a keystroke
// if it is not, then the computer may think that the device
// has stopped working, and give errors
TrinketKeyboard.poll();
digitalWrite(led, LOW);
delay(10);
x= x+1;
if (x > 20)
{
y=y+1;
w = analogRead(sensorPin);
z = w + z;
if (y > 700)
{
digitalWrite(led, HIGH);
v = z / y;
l = (1024 – v);
//if (digitalRead(PIN_BUTTON_CAPITAL_A) == LOW)
// 1
// t4his should type a capital A
//Tri.9nketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_A);
// this4
// releases the key (otherwise it is held down!)
//TrinketKeyboard.pressKey(0, 0);
//}
//if (digitalRead(PIN_BUTTON_STRING) == LOW)
//{
// type out a string using the Print class
sensorValue = analogRead(sensorPin);
// TrinketKeyboard.print(”Hello World!”);
TrinketKeyboard.print(”Value ”);
TrinketKeyboard.print(sensorValue*0.0117);
TrinketKeyboard.print(”\n”);
y=0;
z=0;
x=0;
}
x=0;
}
// delay(20000);
//Hello World!Hello World!Hello World!Hello World!Hello World! }
}
In the raspberry a small python script recives the USB data
import sys
readout=””
fp = open(‘/dev/hidraw0’, ‘rb’)
while True:
buffer = fp.read(8)
for c in buffer:
if ord(c) > 0:
if ord(c) == 30:
readout = readout + ”1”
if ord(c) == 31:
readout = readout + ”2”
if ord(c) == 32:
readout = readout + ”3”
if ord(c) == 33:
readout = readout + ”4”
if ord(c) == 34:
readout = readout + ”5”
if ord(c) == 35:
readout = readout + ”6”
if ord(c) == 36:
readout = readout + ”7”
if ord(c) == 37:
readout = readout + ”8”
if ord(c) == 38:
readout = readout + ”9”
if ord(c) == 39:
readout = readout + ”0”
if ord(c) == 55:
readout = readout + ”.”
if ord(c) == 44:
print readout
f= open(‘/home/pi/volt’, ‘w’)
f.write(readout)
f.close
readout=””
#print ord(c)
#print ”\n”