Thursday, April 24, 2014

Arduino with sensor and ec probe controlled via Python on rpi




//C and C++ code for Arduino

#include <Servo.h>

float Count;

float Voltage;

float SensorReading;

int TimeBetweenReadings = 500; // in ms

int ReadingNumber=0;

float Time;

char Sensor[]="Hand Dynamometer";

float Intercept = -19.295;

float Slope = 175.416;

Servo myservo;  // create servo object to control a servo

Servo claw;               // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

int b=0;

int posit=0; //var for storing claw position

 

void setup(){

  Serial.begin(9600); //initialize serial communication at 9600 baud

Serial.println("Vernier Format 2");

Serial.print(Sensor);

Serial.print(" ");

Serial.println("Readings taken using Ardunio");

Serial.println("Data Set");

Serial.print("Time");

Serial.print("\t"); //tab character

Serial.println ("EC"); //change to match sensor

Serial.print("seconds");

Serial.print("\t"); // tab character

Serial.println ("μS"); //change to match sensor

  myservo.attach(8);

  claw.attach(7); //  the servo on pin 7

 

void loop(){

Serial.print(ReadingNumber/1000.0*TimeBetweenReadings);

Count = analogRead(A0);

Voltage = Count / 1024 * 5.0;// convert from count to raw voltage

SensorReading= Intercept + Voltage * Slope;

Serial.print("\t"); // tab character

Serial.println(SensorReading);

delay(TimeBetweenReadings);// delay in between reads for stability

ReadingNumber++;

  if(Serial.available()>0){

    b=Serial.read();

    serial.print("Data Received System functional");

    if (b="4") {

      for(pos = 0; pos < 360; pos += 1)  

  {                                 

    myservo.write(pos);              

    delay(15);  }

    }

    if (b="6") {

      for(pos = 0; pos < 360; pos += 1)  

  {                                 

    myservo.write(pos);              

    delay(15);  }

    }

   if (b="2") {

      for(pos = 0; pos < 360; pos += 1)  

  {                                 

    claw.write(posit);              

    delay(15);  }

    }

   if (b="6") {

     for(pos = 0; pos < 360; pos += 1)  

  {                                 

   claw.write(posit);              

    delay(15);  }

    }

    

    

 

##script to send keystrokes to arduino

import sys

import serial

Cereal=serial.Serial('/dev/AMCY0/',9600);

 

while 1:

print(Cereal.readline());

   char = sys.stdin.read(1)

Cereal.write(char);

No comments:

Post a Comment