day 11 file i/o on arduino
//Simple Serial Printing Test with a
Potentiometer
const int POT=0; //Pot on analog pin 0
void setup()
{
Serial.begin(9600); //Start serial port with baud = 9600
}
void loop()
{
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage
Serial.print("Analog Reading: ");
Serial.print(val); //Print raw analog value
Serial.print(" Percentage: ");
Serial.print(per); //Print percentage analog value
Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
///// reads percentage output when you turn the dial
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Tabular serial printing test with a potentiometer
const int POT=0; //Pot on analog pin 0
void setup()
{
Serial.begin(9600); //Start Serial Port with Baud = 9600
}
void loop()
{
Serial.println("\nAnalog Pin\tRaw Value\tPercentage");
Serial.println("------------------------------------------");
for (int i = 0; i < 10; i++)
{
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage
Serial.print("A0\t\t");
Serial.print(val);
Serial.print("\t\t");
Serial.print(per); //Print percentage analog value
Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
}
/////// reads the voltage out and the perectagne left until you complete a whole turn.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Echo every character
char data; //Holds incoming character
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
}
void loop()
{
//Only print when data is received
if (Serial.available() > 0)
{
data = Serial.read(); //Read byte of data
Serial.print(data); //Print byte of data
}
}
const int POT=0; //Pot on analog pin 0
void setup()
{
Serial.begin(9600); //Start serial port with baud = 9600
}
void loop()
{
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage
Serial.print("Analog Reading: ");
Serial.print(val); //Print raw analog value
Serial.print(" Percentage: ");
Serial.print(per); //Print percentage analog value
Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
///// reads percentage output when you turn the dial
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Tabular serial printing test with a potentiometer
const int POT=0; //Pot on analog pin 0
void setup()
{
Serial.begin(9600); //Start Serial Port with Baud = 9600
}
void loop()
{
Serial.println("\nAnalog Pin\tRaw Value\tPercentage");
Serial.println("------------------------------------------");
for (int i = 0; i < 10; i++)
{
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage
Serial.print("A0\t\t");
Serial.print(val);
Serial.print("\t\t");
Serial.print(per); //Print percentage analog value
Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
}
/////// reads the voltage out and the perectagne left until you complete a whole turn.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Echo every character
char data; //Holds incoming character
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
}
void loop()
{
//Only print when data is received
if (Serial.available() > 0)
{
data = Serial.read(); //Read byte of data
Serial.print(data); //Print byte of data
}
}
////////writes out character when you input it into the serial moniter
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Single Character Control of an LED
const int LED=9;
char data; //Holds incoming character
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
pinMode(LED, OUTPUT);
}
void loop()
{
//Only act when data is available in the buffer
if (Serial.available() > 0)
{
data = Serial.read(); //Read byte of data
//Turn LED on
if (data == '1')
{
digitalWrite(LED, HIGH);
Serial.println("LED ON");
}
//Turn LED off
else if (data == '0')
{
digitalWrite(LED, LOW);
Serial.println("LED OFF");
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Sending Multiple Variables at Once
//Define LED pins
const int RED =11;
const int GREEN =10;
const int BLUE =9;
//Variables for RGB levels
int rval = 0;
int gval = 0;
int bval = 0;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
//Set pins as outputs
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop()
{
//Keep working as long as data is in the buffer
while (Serial.available() > 0)
{
rval = Serial.parseInt(); //First valid integer
gval = Serial.parseInt(); //Second valid integer
bval = Serial.parseInt(); //Third valid integer
if (Serial.read() == '\n') //Done transmitting
{
//set LED
analogWrite(RED, rval);
analogWrite(GREEN, gval);
analogWrite(BLUE, bval);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Single Character Control of an LED
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
int angle =0;
const int LED=8;
int val; //Holds incoming character
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600); //Serial Port at 9600 baud
pinMode(LED, OUTPUT);
}
void loop()
{
for (angle = 0; angle < 180; angle += 1)
myServo.write(val); //sets the servo
delay(15); //waits for the servo
delay(2000);
for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees
{
myServo.write(val); //command to rotate the servo to the specified angle
delay(5);
}
delay(2000);
//Only act when angle is available
if (Serial.available() > 0)
{
val = Serial.read(); //Read byte of data
//Turn LED on
if (val >= 0)
{
digitalWrite(LED, HIGH);
Serial.println("LED ON");
}
//Turn LED off
else if (val <= 0)
{
digitalWrite(LED, LOW);
Serial.println("LED OFF");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Homework
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
//Define variables
double alt = 0 ;
double v = 0 ;
double startt = 0 ;
double endt = 0 ;
double t = userstartt ;
double inct = 0 ;
//Print title
printf(" Balloon Information \n") ;
for(t = 60; t < 2880; t++) // Convert hours to minutes
{
//Tell and allow the user to input the start time
printf("Enter start time = ");
scanf("%lf", &startt);
//Tell and allow the user to input the end time
printf("Enter endtime = ");
scanf("%lf", &endt);
//Tell and allow the user to input the increment between line for time
printf("Enter Line Increment of Time = ");
scanf("%lf", &inct);
//Print table
printf(" Altitude Velocity \n");
//Find the balloon information for every 10 minutes and print
for(t = startt; t<= endt; t = t + inct)
{
alt = ((-0.12*(pow(t,4)) + (12*(pow(t,3))) - (380*(t*t)) + (4100*t) + 220));
v = ((-0.48*(pow(t,3)) + (36*(t*t)) - (760*t) + 4100));
printf(" %5.2lf m %5.2lf mps \n", alt, v/3600);
} //Close out second while loop
} //Close out first while loop
//Exit program
system("pause");
return (0);
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
//Define variables
double alt = 0 ;
double v = 0 ;
double startt = 0 ;
double endt = 0 ;
double t = userstartt ;
double inct = 0 ;
//Print title
printf(" Balloon Information \n") ;
for(t = 60; t < 2880; t++) // Convert hours to minutes
{
//Tell and allow the user to input the start time
printf("Enter start time = ");
scanf("%lf", &startt);
//Tell and allow the user to input the end time
printf("Enter endtime = ");
scanf("%lf", &endt);
//Tell and allow the user to input the increment between line for time
printf("Enter Line Increment of Time = ");
scanf("%lf", &inct);
//Print table
printf(" Altitude Velocity \n");
//Find the balloon information for every 10 minutes and print
for(t = startt; t<= endt; t = t + inct)
{
alt = ((-0.12*(pow(t,4)) + (12*(pow(t,3))) - (380*(t*t)) + (4100*t) + 220));
v = ((-0.48*(pow(t,3)) + (36*(t*t)) - (760*t) + 4100));
printf(" %5.2lf m %5.2lf mps \n", alt, v/3600);
} //Close out second while loop
} //Close out first while loop
//Exit program
system("pause");
return (0);
}



Comments
Post a Comment