File content as of revision 0:838a1c343613:
#include "mbed.h"
#include "Servo.h"
//#include "BusIn.h"
//#include "DigitalIn.h"
//--------------------------------------------windsensor begin
Timer t;
void Goedzo_staan_groen();
void Rechtsdraaien();
void Linksdraaien();
void Goedzo_BO_groen();
void Boven_draaien();
void Onder_draaien();
void veiligheidsmodus();
void counter();
Serial pc(USBTX, USBRX);
volatile int IRQcount;
// DigitalIn inputpin(p5);//was p2
int pin_irq = 0; //IRQ that matches to pin 2
InterruptIn Windinterrupt(p5);
DigitalIn PMS_PIN(p6);
bool count = false;
int t1 = 0, t2 = 0;
int hits = 0;
int rpp = 0;
int rps = 0;
int pwm = 13;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index1 = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int gemrij1=0;
int Aantalmetingen1 = 20;
//int reeks1[Aantalmetingen1]={0};
const int maxwindpulsen = 20; //Te groot voor te testen
const int MinimaalLicht = 20;
//------------------------------------------- Windsensor einde
Servo myservo(p21);
int pos = 0;
int wachttijd = 1000;
int buttonState = 0; // variable for reading the pushbutton status
DigitalOut linksdraaien (p14);
DigitalOut goedzo (p13);
DigitalOut rechtsdraaien (p12);
DigitalOut bovendraaien (p11);
DigitalOut goedzoBO (p10);
DigitalOut onderdraaien (p9);
AnalogIn Pin (p17); // Analog input pin that the potentiometer is attached to
AnalogIn Pin1 (p18); // Analog input pin that the potentiometer is attached to
AnalogIn Pin2 (p19); // Analog input pin that the potentiometer is attached to
AnalogIn Pin3 (p20); // Analog input pin that the potentiometer is attached to
//const int analogOutPin4 = 9; // Analog output pin that the LED is attached to
const int aantalmetingen = 2;
int rij1[aantalmetingen]= {0};
int rij2[aantalmetingen]= {0};
int rij3[aantalmetingen]= {0};
int rij4[aantalmetingen]= {0};
unsigned long PrevTime=0;
int sensorValue = 0; // value read from the pot
int sensorValue1 = 0; // value read from the pot
int sensorValue2= 0; // value read from the pot
int sensorValue3 = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int ugemiddeld(int*);
int ugemiddeld(int *rijgem)
{
int som=0;
for (int i=0; i<aantalmetingen; i++) {
som=som+rijgem[i];
}
int gemiddeld=(som/aantalmetingen);
return (int)gemiddeld;
}
//-------------------------------------------------------------------------- Windsensor begin
// put your setup code here, to run once:
// attachInterrupt(pin_irq, IRQcounter, RISING);
// put your setup code here, to run once:
//-------------------------------------------------------------------------- Windsensor einde
// initialize the pushbutton pin as an input:
//-------------------------------------------------------------------------- Windsensor begin
void IRQcounter()
{
IRQcount++;
}
long int frq;
//-------------------------------------------------------------------------- Windsensor einde
int main()
{
t.start();
while (1) {
Windinterrupt.rise(&counter);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
//-------------------------------------------------------------------------- Windsensor begin
t2 = t.read();
if (t2 >= (t1 + 1000)) {
rpp = hits/1;// delen door 6 voor aantal rondjes
rps = hits/6;// delen door 6 voor aantal rondjes
hits = 0;
}
// subtract the last reading:
total= total - readings[index1];
// read from the sensor:
readings[index1] = rpp;
// add the reading to the total:
total= total + readings[index1];
// advance to the next position in the array:
index1 = index1 + 1;
// if we're at the end of the array...
if (index1 >= numReadings)
// ...wrap around to the beginning:
index1 = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
// wait_ms(1); // wait_ms in between reads for stability
pc.printf("pulsen per seconde: %d\t\t",rpp);
pc.printf("RPM: %d\t\t",rps*60 );
pc.printf("GEM: %d\n",average);
wait_ms(1000);
// attachInterrupt(0, counter, RISING);
//-------------------------------------------------------------------------- Windsensor einde
// read the state of the pushbutton value:
// read the analog in value:
PrevTime = t.read();
//while (PrevPrevPrevPrevTime==wachttijd)//variabele van maken!!!
for (int huidigemeting=0; huidigemeting <= aantalmetingen; huidigemeting++) {
wait_ms(10);
rij1[huidigemeting] = Pin.read();
rij2[huidigemeting] = Pin1.read();
rij3[huidigemeting] = Pin2.read();
rij4[huidigemeting] = Pin3.read();
}
sensorValue = Pin.read();
sensorValue1 = Pin1.read();
sensorValue2 = Pin2.read();
sensorValue3 = Pin3.read();
// map it to the range of the analog out:
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en3=(gemrij1 + gemrij3)/2;
int gemrij2en4=(gemrij2 + gemrij4)/2;
int gemrij1en2=(gemrij1 + gemrij2)/2;
int gemrij3en4=(gemrij3 + gemrij4)/2;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Te weinig Ligt
int TeWeinigLicht = (gemrij1 + gemrij2 + gemrij3 + gemrij4)/4;
pc.printf("------------------------------------" );
pc.printf("sensorGem = %d, %d, %d, %d \n " ,gemrij1, gemrij2, gemrij3, gemrij4 );
// check if the pushbutton is not pressed.
// if it is, the buttonState is LOW
if ( average <= maxwindpulsen && TeWeinigLicht >= MinimaalLicht)
{
if(gemrij2en4 > gemrij1en3-10&&gemrij2en4 < gemrij1en3+10) {
Goedzo_staan_groen();
wait_ms(10);
}
if(gemrij2en4 > gemrij1en3+10) {
Rechtsdraaien();
} else {
if(gemrij1en3 > gemrij2en4+10) {
Linksdraaien();
}
}
if(gemrij1en2 > gemrij3en4-10&&gemrij1en2 < gemrij3en4+10) {
Goedzo_BO_groen();
}
if(gemrij1en2 > gemrij3en4+10) {
Boven_draaien();
} else {
if(gemrij3en4 > gemrij1en2+10) {
Onder_draaien();
}
}
}
else {
veiligheidsmodus();
}
}
}
void Goedzo_staan_groen()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en3=(gemrij1 + gemrij3)/2;
int gemrij2en4=(gemrij2 + gemrij4)/2;
pc.printf("Is goed zo! Verschil links rechts = %d , Pos = %d \n", gemrij2en4-gemrij1en3, pos);
// wait_ms(10)(1000);
rechtsdraaien = 0; // turn the LED off by making the voltage LOW
goedzo = 1; // turn the LED off by making the voltage LOW
linksdraaien = 0; // turn the LED off by making the voltage LOW
}
void Linksdraaien()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en3 = (gemrij1+gemrij3)/2;
int gemrij2en4 = (gemrij2+gemrij4)/2;
if(gemrij1en3 > gemrij2en4+10) {
pos+=2;
myservo.write(pos);
pc.printf("Sensor1en3 = %d < Sensor2en4 = %d \n ",gemrij1en3, gemrij2en4);
// wait_ms(10)(1000);
rechtsdraaien = 0; // turn the LED off by making the voltage LOW
goedzo = 0; // turn the LED off by making the voltage LOW
linksdraaien = 1; // turn the LED off by making the voltage LOW
}
}
void Rechtsdraaien()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en3 = (gemrij1+gemrij3)/2;
int gemrij2en4 = (gemrij2+gemrij4)/2;
pos-=2;
myservo.write(pos);
pc.printf("Sensor1en3 = %d > Sensor2en4 = %d \n ",gemrij1en3,gemrij2en4);
// wait_ms(1000);
rechtsdraaien = 1; // turn the LED off by making the voltage LOW
goedzo = 0; // turn the LED off by making the voltage LOW
linksdraaien = 0; // turn the LED off by making the voltage LOW
}
void Goedzo_BO_groen()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en2=(gemrij1 + gemrij2)/2;
int gemrij3en4=(gemrij3 + gemrij4)/2;
pc.printf("Is goed zo! Verschil boven onder = %d , Pos = %d",gemrij1en2-gemrij3en4,pos );
wait_ms(1000);
onderdraaien = 0; // turn the LED off by making the voltage LOW
goedzoBO = 1; // turn the LED off by making the voltage LOW
bovendraaien = 0; // turn the LED off by making the voltage LOW
}
void Onder_draaien()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en2=(gemrij1 + gemrij2)/2;
int gemrij3en4=(gemrij3 + gemrij4)/2;
if(gemrij3en4 > gemrij1en2+10) {
//pos+=2;
//myservo.write(pos);
pc.printf("Sensor3en4 = %d < Sensor1en2 = %d ", gemrij3en4, gemrij1en2);
// wait_ms(1000);
onderdraaien = 0; // turn the LED off by making the voltage LOW
goedzoBO = 0; // turn the LED off by making the voltage LOW
bovendraaien = 1; // turn the LED off by making the voltage LOW
}
}
void Boven_draaien()
{
int gemrij1=ugemiddeld(rij1);
int gemrij2=ugemiddeld(rij2);
int gemrij3=ugemiddeld(rij3);
int gemrij4=ugemiddeld(rij4);
int gemrij1en2=(gemrij1 + gemrij2)/2;
int gemrij3en4=(gemrij3 + gemrij4)/2;
//pos-=2;
//myservo.write(pos);
pc.printf("Sensor3en4 = %d > Sensor1en2 = %d", gemrij3en4, gemrij1en2);
// wait_ms(1000);
onderdraaien = 0; // turn the LED off by making the voltage LOW
goedzoBO = 0; // turn the LED off by making the voltage LOW
bovendraaien = 1; // turn the LED off by making the voltage LOW
}
void veiligheidsmodus()
{
while (pos <= 180) {
// goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
wait_ms(15);
pos += 1;
// GROOT wait_ms VOOR WACHTEN TOT WEER OMHOOG GAAN OFTEWEL VOLGENDE METING
}
}
//-------------------------------------------------------------------------- Windsensor begin
void counter(void)
{
if (PMS_PIN.read() == 1) {
if (!count) {
count = true;
hits++;
}
count = false;
} else {
count = false;
}
}
//-------------------------------------------------------------------------- Windsensor einde