Second date with Arduino

That’s the second date and I’m not still sure about my feelings for Arduino.I Don’t know what to think because I’m attracted to it but I’m scared it’s going to use me .

Anyway, here I show you some of the stuff I made today , let’s start with the Led Fading:

//scopo dell'esercizio far fare il fade al led, ossia farlo illuminare fino al massimo della potenza e e poi farlo diminuire.
//come si fa?
// si impostano i valori:
int ledPin=11;// qua è da dove esce il filo verso la breadboard
int ledVal=10;//valore del led
int x=0;// in questo caso 0 equilvale al valore più basso che il led può avere, in questo caso spento. Il più alto valore a cui il led può arrivare è 255, quando si illumina al massimo.

void setup () {
pinMode (ledPin, OUTPUT);
}
void loop () {
for (int x=0; x<255; x++){;//per ogni volta che x è uguale a 0 o minore di 255 aggiungi i valori analogWrite (ledPin,x); delay (5); } for (int x=255; x>0; x–)// al contrario: per ogni volta che x è uguale a 255 e maggiore di zero sottrai i valori
{ //
analogWrite (ledPin,x);
delay (5);
}

}
// quindi io comando al led di illuminarsi sempre di più (fino al massimo 255) e una volta raggiunto il massimo di illuminarsi sempremeno sottraendo i valori.


you can use also another code,using the variable “direction”:
int ledPin = 11;
int direction = 1;

void setup (){
pinMode(ledPin, OUTPUT);
}
int x=0;
void loop () {
for(int x=0;x<255;x++) {

if(direction==1){
analogWrite(ledPin,x);
}
if (direction==1){
analogWrite(ledPin,255-x);
delay (5);
}
direction*= -1;
}
}

Now move to the use of a potentiometer, you can find it in the arduino kit or go and buy it now.

int sensorPin = 2;
int ledPin =11;
int sensorValue = 0;
void setup () {

Serial.begin (9600);
}
void loop () {
sensorValue = analogRead (sensorPin);
int ledFadeValue = map (sensorValue, 0, 1023, 0, 255);
analogWrite (11, ledFadeValue);
Serial.println (sensorValue, DEC);
delay (20);
}

buzz and led with a button, you can use both them with the same code, change the name buzzPin-ledPin, ledState/buzzState:
int buttonPin = 2;
int buzzPin = 13;
int buttonState = 0;

void setup() {
pinMode (buzzPin, OUTPUT);
pinMode (buttonPin, INPUT);
}
void loop () {
buttonState = digitalRead (buttonPin);
if (buttonState==HIGH){
//turn led on
digitalWrite (buzzPin, HIGH);

}
else{
digitalWrite(buzzPin, LOW);

}
}

it was easy, just push a button on the breadboard and make light up the led on arduino,when you release the button the led is switched off. But a blonde would say: “as a broke button! Cause when you turn a light on the light just stay on until you don’t switch it off”. Right and what a blonde would do once left alone reflecting on a way to modified the previous code and “make the button works correctly”?.

That’s the result:
int buttonPin = 2;
int ledPin = 13;
int buttonState = 0;

void setup() {
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
}
void loop () {
buttonState = digitalRead (buttonPin);
if (buttonState==HIGH){
//turn led on
digitalWrite (ledPin, LOW);

}
else {
//turn led off:
digitalWrite(ledPin, HIGH);

}
}

As a blonde I’ve just refused to understand the feelings of the led ordering to it to shut up its F* mouth when the button was pushed and to be delighted when the button was pushed again, so disorienting the led in a sort of internal fight.. worked out! yuppie!
but the real solution is different:

int LED =13 ;
int BUTTON =2;

int val = 0;
int old_val = 0;

int state = 0;
void setup () {
pinMode (LED, OUTPUT);
pinMode (BUTTON, INPUT);
}
void loop () {
val = digitalRead (BUTTON) ;
if ((val == HIGH) && (old_val == LOW)){
state = 1-state;
delay (10);
}
old_val = val;
if (state==1){
digitalWrite (LED,HIGH);
}else{
digitalWrite (LED, LOW);
}
}

Annunci