Day 16

#include<stdio.h>
#include<math.h>
#define FILENAME "ENS01.txt"
#define MAX_SIZE 1000
int main(void)
{
int k=0, year[MAX_SIZE], qtr[MAX_SIZE], max_k=0,nor_k=0;
double index[MAX_SIZE];
FILE *enso;
//READ SENSOR DATA FILE
enso = fopen(FILENAME,"r");
if(enso==NULL)
printf("Error opening input file. \n");
else
{
while(fscanf(enso,"%d %d %lf",
year+k,qtr+k,index+k)==3)
{

if(*(index+k)>*(index+max_k))
max_k=k;
if(fabs(*(index+nor_k))> fabs(*(index+k)))
nor_k=k;
k++;
}

}
//print data for max el nion condition
printf("Maximun EL Nion Conditions in Dara File \n");
printf("Year: %d, Quarter: %d \n",
*(year+max_k),*(qtr+max_k));


printf("EL Nion Conditions closest to normal in Dara File \n");
printf("Year: %d, Quarter: %d \n\n",
*(year+nor_k),*(qtr+nor_k));


//Printout all
for(int i=0;i<k;i++)
printf("Year: %d, Quarter: %d, ENSO Index %.1f \n",
*(year+i),*(qtr+i),*(index+i));





//close file
fclose(enso);
return 0;


}
For the lab we had to control the motor with serial commands here's the code
int M1_Left = 6; //Motor Input 1
int M1_Right = 5;
int M2_Left = 10;
int M2_Right = 9;
boolean inPin1 = LOW;
  boolean inPin2 = HIGH;
  boolean inPin5 = LOW;
  boolean inPin6 = HIGH;
char data;
//Motor Input 2
int val = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(M1_Left, OUTPUT);
  pinMode(M1_Right , OUTPUT);
  pinMode(M2_Right, OUTPUT);
  pinMode(M2_Left, OUTPUT);
  //
  void turn(int direction);
}
void loop() {
//  val = analogRead(A0);
//
//  val = map(val, 0, 1023, -100, 100);
//  if (val < -3)
//  {
//    turn (1);
//    delay(-val); //1 sg
//    stop();
//    delay(20);
//  }
//  else if (val > 3)
//  {
//    turn (2);
//    delay(val); //1 sg
//    stop();
//    delay(20);
//  }
//  else
//    stop();
//}

  while (Serial.available() > 0)
  {
    data = Serial.read();


  if (data == '1')
    {
    
  
     forward();
     Serial.println("FORWARD");

    

    }
  if (data== '2' )
  {
    reverse();
    Serial.println("REVERSE");
  }
    
if (data=='4')
{
  Serial.println("speedUP");
  speedup(val+2);


}



   if (data=='5')
   {
    speeddown(val+2);
    Serial.println("speeddown");
  
   }

     if  (data == '0')
    {
      stop();
      Serial.println(" STOP");
      val==0;
    }
if(val>100|val<-100)
val=0;

//    if (data=='2')
//    {
//        turn (2);
//    delay(val); //1 sg
//    stop();
//    delay(20);
//    Serial.println("REVERSE");
//    }






}
}
void turn(int direction)
{
  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;
  boolean inPin5 = LOW;
  boolean inPin6 = HIGH;
  if (direction == 1) {
    inPin1 = HIGH;
    inPin2 = LOW;
    inPin5 = HIGH;
    inPin6 = LOW;
  }

  digitalWrite(M1_Left, inPin1);
  digitalWrite(M1_Right , inPin2);
  digitalWrite(M2_Left, inPin5);
  digitalWrite(M2_Right , inPin6);
}
void stop()
{
  digitalWrite(M1_Left, LOW);
  digitalWrite(M1_Right , LOW);
  digitalWrite(M2_Left, LOW);
  digitalWrite(M2_Right, LOW);
}
void forward()
{
  digitalWrite(M1_Left,HIGH);
  digitalWrite(M1_Right,LOW);
   digitalWrite(M2_Left, HIGH);
  digitalWrite(M2_Right, LOW);

}
void reverse()
{
   digitalWrite(M1_Left,LOW);
  digitalWrite(M1_Right,HIGH);
   digitalWrite(M2_Left, LOW);
  digitalWrite(M2_Right, HIGH);

}

void speedup(int val)
{

    forward();
    delay(val); //1 sg
    stop();
    delay(20);
}
void speeddown(int val)
{

    reverse();
    delay(val); //1 sg
    stop();
    delay(20);
}

Comments

Popular posts from this blog

Homework 6

Another Day 23

Homework4