top of page
elnazkhosro68

Software and coding

Updated: Jan 23, 2019

Microcontrollers are widely used in embedded systems and make devices work according to our needs and requirements. Arduino Uno is a very valuable addition in the electronics that consists of USB interface, 14 digital I/O pins, 6 Analog pins, and Atmega328 microcontroller. It also supports serial communication using Tx and Rx pins. In this project the controller unit is Arduino Uno R3. Arduino Uno can be easily programmed and it is a very good economical option. The software used for Arduino devices is called IDE (Integrated Development Environment) which is free to use and required some basic skills to learn it. It can be programmed using C and C++ language. The codes can be transferred directly to the controller through USB cable .







Arduino Uno R3



Source codes

. const int FLEX_PIN = A0; // Pin connected to voltage divider output

int led=13;

int gled= 11 ;

int yled=7;

int out =4;

//int count;

const float VCC = 4.98; // Measured voltage of Ardunio 5V line

const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor

// Upload the code, then try to adjust these values to more

// accurately calculate bend degree.

const float STRAIGHT_RESISTANCE = 37300.0; // resistance when straight

const float BEND_RESISTANCE = 90000.0; // resistance at 90 deg

void setup()

{

Serial.begin(9600);

pinMode(FLEX_PIN, INPUT);

pinMode(led, OUTPUT);

pinMode(gled,OUTPUT);

pinMode(yled,OUTPUT);

pinMode(out, OUTPUT);

}

void loop()

{

// Read the ADC, and calculate voltage and resistance from it

//int flexSensorValue = analogRead(FLEX_PIN);

int flexADC = analogRead(FLEX_PIN);

float flexV = flexADC * VCC / 1023.0;

float flexR = R_DIV * (VCC / flexV - 1.0);

Serial.println("Resistance: " + String(flexR) + " ohms");

// Use the calculated resistance to estimate the sensor's

// bend angle:

float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,

0, 90.0);

Serial.println("Bend: " + String(angle) + " degrees");

//angle= digitalRead(FLEX_PIN);

//int i=0;

unsigned char i, j ;

//float angle;

if (angle>80 && angle<90)

{

//count=count+1;

digitalWrite(gled,HIGH);

digitalWrite(led,LOW);

Serial.println(" Green Light in on");

digitalWrite(yled, HIGH); // turn the LED on (HIGH is the voltage level)

delay(150); // wait for a second

digitalWrite(yled, LOW); // turn the LED off by making the voltage LOW

delay(100);

//digitalWrite(out,LOW);

//delay(100);

} else{

digitalWrite(led,HIGH);

digitalWrite(gled,LOW);

digitalWrite(yled,LOW);

//digitalWrite(out,HIGH);

for (i = 0; i <20; i++){ // 100 cycles of sound

digitalWrite (out, HIGH); // This will turn the buzzer ON

delay (2) ; // Giving a delay of 2ms will set frequency 2

digitalWrite (out, LOW); // This will turn the buzzer OFF

delay (2) ; // Giving a delay of 2ms

}

}

switch (angle>75){

//if (angle > 75) {

digitalWrite (out,HIGH);

break;

}

//} else {

//analogWrite (out, LOW);

//}

Serial.println("You are done with",count,"rounds");

//delay(500);

}





33 views0 comments

Recent Posts

See All

Hardware

Comments


bottom of page