//build file
echo 60 > /sys/class/gpio/export
echo 30 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio60/direction
echo "out" > /sys/class/gpio/gpio30/direction
/Program to sense using the sensor
#include <stdio.h> //header files
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <time.h>
int jeet(char *string){ //function to change array to interger
int value = atoi(string);
return value;
}
int main()
{
int x=0;
char c;
int fd = open("/sys/class/gpio/gpio60/value", "r");//open the value directory in read mode
while(1)
{
char buffer[1];
int r = read (fd, buffer,sizeof(buffer));//read the value from the fd and store it in r
//printf("value:%d\n",r);
if(r != '\0' ){ //read the value of r
buffer[r] = '\0';
int vin = jeet(buffer); //stored buffer value will be converted and stored in the int value vin
printf("val:%d\n", vin);//print that value on the screen
if(vin==1){
printf("\"1\"high\n");
system("echo 0 > /sys/class/gpio/gpio30/value"); //if the read value is high then redirect 0 to value file
}
else
{
printf("\"0\"low\n");
system("echo 1 > /sys/class/gpio/gpio30/value");//if it s low then give 1 to value file
}
lseek(fd,0,0);// move the filedescriptor to the starting position of the file
}
sleep(1);//sleep for a time period
}
close(fd);//close the filedescriptor
}
echo 60 > /sys/class/gpio/export
echo 30 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio60/direction
echo "out" > /sys/class/gpio/gpio30/direction
/Program to sense using the sensor
#include <stdio.h> //header files
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <time.h>
int jeet(char *string){ //function to change array to interger
int value = atoi(string);
return value;
}
int main()
{
int x=0;
char c;
int fd = open("/sys/class/gpio/gpio60/value", "r");//open the value directory in read mode
while(1)
{
char buffer[1];
int r = read (fd, buffer,sizeof(buffer));//read the value from the fd and store it in r
//printf("value:%d\n",r);
if(r != '\0' ){ //read the value of r
buffer[r] = '\0';
int vin = jeet(buffer); //stored buffer value will be converted and stored in the int value vin
printf("val:%d\n", vin);//print that value on the screen
if(vin==1){
printf("\"1\"high\n");
system("echo 0 > /sys/class/gpio/gpio30/value"); //if the read value is high then redirect 0 to value file
}
else
{
printf("\"0\"low\n");
system("echo 1 > /sys/class/gpio/gpio30/value");//if it s low then give 1 to value file
}
lseek(fd,0,0);// move the filedescriptor to the starting position of the file
}
sleep(1);//sleep for a time period
}
close(fd);//close the filedescriptor
}
No comments:
Post a Comment