Hi, im fairly new to this and i have a question that is probably an easy fix

Instead of trying yo explain everything it will be able to show you my code and then tell you the problem...

int ADC0 = 0;
int value;
int newValue;
int LEDpin = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, 1);
  value = analogRead(ADC0);
}

void loop() {
  newValue = analogRead(ADC0);
  
  if(value == newValue){
    digitalWrite(LEDpin, 1);
    
  }else{
    digitalWrite(LEDpin, 0);
    value = newValue;
    
    Serial.print("Time: ");
  String valueS = String(value);
  String timer = String(valueS + "0ms");
  Serial.println(timer);
  }
  delay(1000);
  
}

This is the code I have i want to output the the value only when the value has been changed the problem I'm having is getting a starting value that does not reset every second when the loops resets it also resets the value so its not got a value to compare against to see weather it has changed.

The output I'm getting is this...
Time: 6820ms
Time: 00ms
Time: 6830ms
Time: 00ms
Time: 6820ms
Time: 00ms
Time: 6820ms
Time: 00ms
Time: 6820ms
Time: 00ms

Thanks

Hi jackt96

int ADC0 = 0;
...
value = analogRead(ADC0);

You are measuring an analog voltage on pin A0. What do you have connected to that pin?

Regards

Ray

Hi Ray,
I have a 10k pot attached to pin A01.....
Thankyou for the suggestion i have tried it and it does not seem to work still. I have commented all the rest of the loop out and just printed the value and i am getting a value of 0.

int ADC0 = 0;
int value = analogRead(ADC0);
int newValue;
int LEDpin = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, 1);
}

void loop() {
  Serial.println(value);
  delay(1000);
  
}

Thanks,
Jack

What is more why do you think a voltage reading is any sort of measure of time?

Have you connected the wiper to the analogue input. And one end of the pot to ground and the other end of the pot to 5V?

Everything is wired up correctly, I can get a reading from the pot. I followed a tutorial from a book for the first part i am now just trying to practise and build up my skills in both the programing side and the electronic sides. What i am trying to achieve from this is for the pot to read back a reading which I have outputted as a time, then I am planning on adding a button and using the time variable set from the pot to set a timer and once that has timed out for a buzzer to go off. I hope this helps understand what I am doing.

Regards
Jack

I have a 10k pot attached to pin A01

A0 or A1?

Are you using a Uno?

EDIT

And I think I confused you with my extract from your code. I didn't mean for you to move the analogRead() statement up front like that :slight_smile: Move it into your cut down loop() code before you print the value.

Sorry A0, and yeah.
Regards
Jack

Had a play around with the code and came up with this..

int ADC0 = 0;
int value = 0;
int newValue = 0;
int LEDpin = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, 1);

}

void loop() {
   
  if(value == newValue){
    digitalWrite(LEDpin, 1);
    newValue = analogRead(ADC0);
  }else{
    digitalWrite(LEDpin, 0);
    value = newValue;
    
    Serial.print("Time: ");
  String valueS = String(value);
  String timer = String(valueS + "0ms");
  Serial.println(timer);
  }
  delay(1000);
  
}

This is now working how I wanted it to, thank you all for your input and help.

  if(value == newValue){
    digitalWrite(LEDpin, 1);
    newValue = analogRead(ADC0);

First, your code would be a lot less confusing if you used names like currValue for the current value and prevValue for the previous value.

Second, it makes no sense to compare value and newValue when you have not assigned values to either one. The FIRST thing to do in loop is to assign a value to currValue (newValue). Then, compare that to prevValue (value).

Finally, the copy of currValue (newValue) to prevValue (value) should be unconditional.

void loop()
{
    currValue = analogRead(ADC0);
    if(currValue != prevValue)
   {
      // do something
   }
   prevValue = currValue;
}