The first step - making an experiment with sensor

According to my final project idea, the first step for the final project is making code for touching with sensor. touching mechanism is like this. When people touch a plant-pet, the pet will recognize care. Then, 9times touch will make a growth little by little.

1. A sensor experiment with LED

ir-data-example2.jpg <<IR sensor data graph>>

Regardless of any sensors, depending on how many times i touched sensor, LED will be getting brighter and dimmer.

<code>

int analogValue; // values from a sensor
int analogPin; // pin 0
int count =0; // whenever coming to the first value, count
int countForLed; // whenever count be the odd, another counter count
long lastTimeValue = 0;
long currentTimeValue = 0;
int pressedStart = false;
int blinkPin = 3; // analog output pin
int brightness; // degree for change

long lastValue[2]; // previous value and time
long currentValue[2]; // current value and time

void setup(){
Serial.begin(9600);
lastValue[0] = currentValue[0] = analogValue; // put the analogvalue in first array - setting
lastValue[1] = currentValue[1] = millis(); // put the time in the second array - setting
}

void loop(){
analogValue = analogRead(analogPin); // anaogread

lastValue[0] = currentValue[0]; // put the current value into the last value
currentValue[0] = analogValue; // put the analog value from analogread into the current value
lastValue[1] = currentValue[1]; // put the current time value into the last time value
currentValue[1] = millis(); // put the time value from millis() into the current time value

// if I get the analogvalue and the first value , count
if(analogValue > 3 && lastValue[0] * currentValue[0] == 0){
count++;
//lastTimeValue = millis() ;
currentTimeValue = currentValue[1] ; // 첫번째 아날로그밸류가들어오면 그떄의 시간을 다른곳에 저장

if(count % 2 ==1){ // and count become the odd number
countForLed++; // countforled be counted
}
}

// output depending on the how many times do i touch sensor
analogWrite(blinkPin, brightness); // analog write
if( countForLed >3){ // countforled become over 3
if(brightness<255){ // until the brightness become 255
brightness +=10; // add 10 and sum up
}
if(brightness >=255){ // if brightness become over 255,
brightness =255; // brightness become 255
count =0; // count zero
}
countForLed = 0; // if countforled become over 3, count become 0
}

if (millis() - currentTimeValue > 1000){
brightness– ;
count = 0;
if(brightness < 0){
brightness = 0;
}
}

// print all values
Serial.print(” analogValue : “);
Serial.print(analogValue);
Serial.print(” count : “);
Serial.print(count);
Serial.print(” countForLed : “);
Serial.print(countForLed);
Serial.print(” brightness : “);
Serial.print(brightness);
Serial.print(” currentTimeValue : “);
Serial.print(currentTimeValue);
Serial.print(” millis : “);
Serial.println(millis());

}

2. A sensor experiment with servo motor

Unlike LED, a servo motor needs to be considered with time values. And, Because touching is based on the time- if I didn’t touch for 3 second, led was getting dimmer-, I was confused about how to make code with servo at first. However, once i got the concept for controlling servo motor, I made the code. For controlling servo motor, minimum pulse, maximum pulse, and refresh time( 20 micro second for this servo) is needed. For looking for the angle and putting it on exact position, digital write is needed as well.

<code>

int analogValue; // values from a sensor
int analogPin; // pin 0
int count =0; // whenever coming to the first value, count
int countForServo; // whenever count be the odd, another counter count
int blinkPin = 13;
int brightness;

long currentTimeValue = 0; // values for checking time when values comes
boolean touched = false; // values for whether it’s touched or not
int servoPin = 2; // analog output pin

int refreshTime = 20; // 20msec for servo
int i=0;

int miniPulse = 600; // miniPulse
int maxPulse = 2400; //maxpulse
int servoPulse = miniPulse; // put the minipulse into the servoPulse
long lastPulse = 0; //

void setup(){
Serial.begin(9600);
pinMode(servoPin,OUTPUT);
pinMode(blinkPin,OUTPUT);
}

void loop(){
analogValue = analogRead(analogPin); // anaogread

// if I get the analogvalue and the first value , count
if(analogValue > 20 && touched == false){ // if anaogValue comes in and touched was not changed yet(first value)
count++; // counting
currentTimeValue = millis(); // 그 때의 밀리스값을 currentTimeValue에 저장하기
if(count % 3 == 0){ // 3번 터치할때마다
countForServo++; // 모터를 위한 카운트를 센다
}
touched=true; // 그리고 터치는 바로 false가 된다.
}else if (analogValue <= 20){ // 그렇지 않고 analogValue가 20보다 작으면
touched = false; // 터치불린 밸류를 false로 만듬
}

// output depending on how many times i touch sensor
// analogWrite(blinkPin, servoPulse); // analog write // 서보모터는 아날로그 라이트로 쓸수 없다.
if(countForServo > 3){ // countforservo becomes over 3

digitalWrite(blinkPin,HIGH);
delay(servoPulse);
digitalWrite(blinkPin, LOW);

if(servoPulse < maxPulse){ // until the pulse become 2400
servoPulse += 50; // add 50 and sum up
if(millis() - lastPulse >= refreshTime){ // if lastPulse is over 20
digitalWrite(servoPin,HIGH); // digitalWrite High
delayMicroseconds(servoPulse); // same as servopulse’s length, delay(pwm)
digitalWrite(servoPin, LOW); // digitalWrite Low
lastPulse = millis(); // put the time into lastPulse
}
}

if (servoPulse >= maxPulse){ // if brightness become over 2400,
servoPulse = maxPulse; // brightness become 2400

if(millis() - lastPulse >= refreshTime){ // 그 펄스대로 딜레이
digitalWrite(servoPin,HIGH);
delayMicroseconds(servoPulse);
digitalWrite(servoPin, LOW);
lastPulse = millis();
}
count =0; // make the count zero
}
countForServo = 0; // if countforled become over 3, count become 0
}

if( millis() - currentTimeValue > 3000){ // if it has not gotten any value 3 seconds later from currentTimeValue,

if(servoPulse > miniPulse){ // substract 5 and sum up
servoPulse -= 5;

if(millis() - lastPulse >= refreshTime){ // digitalWrite
digitalWrite(servoPin,HIGH);
delayMicroseconds(servoPulse);
digitalWrite(servoPin, LOW);
lastPulse = millis();
}
}

if(servoPulse <=miniPulse){ // if servoValue becomes below 600
servoPulse = miniPulse; // value becomes 600
}
}

// print all values
Serial.print(” analogValue : “);
Serial.print(analogValue);
Serial.print(” count : “);
Serial.print(count);
Serial.print(” countForServo : “);
Serial.print(countForServo);
Serial.print(” servoPulse : “);
Serial.print(servoPulse);
Serial.print(” currentTimeValue : “);
Serial.print(currentTimeValue);
Serial.print(” millis : “);
Serial.println(millis());
}

Leave a Comment

You must be logged in to post a comment.