Go Down

Topic: save value (in a simple Code) (Read 6003 times) previous topic - next topic

LLucas

#15
Sep 22, 2015, 01:33 am Last Edit: Sep 25, 2015, 03:19 pm by LLucas

Here is the solution


Guys, I have a new concept to realize my project but I need your help.
I create a Arrays in my code. There a two fields / two measurement:
Val[0] = x / the first measurement from analog pin
Val[1] = y / the second measurement from analog pin

If the two value unequal
{
int pwm = (y / 4);
analogWrite(ledPin, pwm);
}

The Idea is to compare two consecutive value instead of to save the value (if you see my first code concept)
Here is the code but it doesn't work, the arduino programm displays an error:

"
sketch_sep22a:2: error: initializer fails to determine size of 'val'
sketch_sep22a:2: error: array must be initialized with a brace-enclosed initializer
initializer fails to determine size of 'val'
"
Code: [Select]

int ledPin = 9;      // LED connected to digital pin 9
float val[] = 1;    // potentiometer connected to analog pin 1
void setup()
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);             // opens serial port, sets data rate to 9600 bps
}
void loop()
{
if (val[0] != val[1])              // compares the values
    {
     int pwm = (val[1] / 4);   //create pwm-signal
analogWrite(ledPin, pwm);  
     Serial.println(pwm);        
    }
}

Whandall

Here is the code but it doesn't work,
You don't know whether your code works because it does not compile.

Using floats without reason is not overly clever.
Analog does not mean floating point.

Analog values have to be read before they can be used.

This will at least compile:

Code: [Select]
const byte ledPin = 9;      // LED connected to digital pin 9
const byte anaPin = A1;  // potentiometer connected to analog pin 1
byte val[2];
   
void setup()
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);        // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  val[0] = analogRead(anaPin);
  val[1] = analogRead(anaPin);
  if (val[0] != val[1])
    {
     int pwm = (val[1]>>2);
     analogWrite(ledPin, pwm);
     Serial.println(pwm);       
    }
}


Rethink your logic.

If the analogRead would not fluctuate (a litte) two direct consecutive reads
would show no difference (unless you turn the poti very fast).


Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

LLucas

Thank you at all that you try to help me but i give up now.
i haven't any ideas any more.

i have the same problem all the time with any code modification:



best regards
 :)

Whandall

Don't give up.

Learn C (by reading books, watching tutorials, programming),
it's really not that hard and there is no way around it.





Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

LLucas

#19
Sep 22, 2015, 03:20 am Last Edit: Sep 22, 2015, 03:21 am by LLucas
i will start studying c with "C Primer Plus (Developer's Library)"  because i will start my study in mrz 2016 in a university

C Primer Plus is a huge Literature :D

Whandall

That's a very good start.

Without basics you are lost,
...and not only while programming.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

LLucas

#21
Sep 25, 2015, 03:14 pm Last Edit: Sep 25, 2015, 04:05 pm by LLucas
This Code woks:
The Analog-Signal from Poti Jumps and I integrated an average function who measures 100 values. 

Code: [Select]

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 5;   // potentiometer connected to analog pin 1
float pwmOLD = 0;    //  variable to store the read value
float pwmNEW = 0;         //  variable to store the read value   

void setup()   
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}
 
void loop()
{
  // calculate average
  // average from 100 measurements
    pwmNEW=0;
      for(int i=0;i<100;i++)
      {
       
        pwmNEW+=(analogRead(analogPin)/4);
      }
      pwmNEW=trunc(pwmNEW/100);
   
      if (pwmNEW != pwmOLD)              // verleicht die Werte valNEW und valOLD
            {             
               analogWrite(ledPin, pwmNEW);  // PWM-Signal send to LED-Pin   
               Serial.print("                     PWM-Signal:"); // PWM-Signal on S-Monitor if available
               Serial.println(pwmNEW);     
               pwmOLD = pwmNEW;  // Put pwm value as "valOLD"  AND save this val for the next pass
             }
  Serial.print("Analogsignal:"); // Analog-Signal on S-Monitor if available
  Serial.println(analogRead(analogPin));
           
}

aarg

What exactly do you mean "jumps"? Varies enough to be seen as a brightness fluctuation of the LED? That isn't a known problem with this hardware. This kind of experiment is very common on Arduino.

What happens when you run the example sketch "AnalogInOutSerial" that comes with the IDE?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Go Up