Needing help with this.

hello guys
I am very new to this and am looking for some assistance with this project I am trying to make.
I have been using tinker cad to learn but I am stuck in designing a system with
1 red led
1 green led
1 button
1 potentiometer
1 push button
to make a PWM
the lights need to dim and brighten to and from the halfway point of the potentiometer
it,s mostly the coding I am stuck with I am a very poor programmer,
my teacher has advised me to do this but it is no help
PLEASE HELP GUYS Desperate

Use the potentiometer to set the maximum (desired) distance
Once you have a value for the potentiometer, you will need
to convert it to a distance e.g. is your maximum distance is
300, the potentiometer has a maximum of 1024, so you will need to divide by 3
Based on how far the actual_Distance is from the desired_Distance
If actual_Distance is < increase the brightness of Green LED
If actual_distance is > decrease brightness of Green LED and increase the brightness of RED lED
This brightness needs to be proportionate to the difference in distance
so do distance = desired_Distance - actual_Distance
then see if this value will set your brightness correctly like
Remember to use Serial.println to find out what values you are producing.
Eventually, you will need a button on an interrupt. Then when the button is clicked this will accept the Potentiometer value

Rather than have us do your entire homework assignment, perhaps you could show what you have already tried and where you are stuck.

Sorry but your "specification" make almost no sense.

What does distance have to do with anything? What distance from where? How do you know actual_Distance and desired_Distance? And if you're having a button why does it need an interrupt? Why can't you just scan it like normal people do?

Have you actually tried to write any code to do this? If so then post your best attempt. If not then try out some examples from the IDE which have plenty of examples of things like reading a pot and changing brightness of an LED.

Steve

thank you for your comments guy this is not my homework
this is a project I am doing through uni and one of my classes is software engineering and this year we are doing it with Arduino's last term was Raspberry Pi
but thanks
not very good at programming
but any advice would be good to put me on the right path

Develop a prototype embedded system using the Arduino to implement an adaptive cruise control system. The basic specification is as follows:
A method of setting the desired distance. (HAVE THIS AND WORKING)
A button to confirm a new setting
Collision Warning (HAVE THIS AND WORKING)
Cancel setting/exit ACC
LCD displays mode and set-distance and the actual distance
Engine Acceleration (PWM output to LED) Brake Control (PWM output to LED)

#define trigPin 13
#define echoPin 12
const byte green1LED = 2;
const byte green2LED = 3;
const byte yellow1LED = 4;
const byte yellow2LED = 5;
const byte red1LED = 6;
const byte red2LED = 7;

const byte redPin = 10;
const byte greenPin = 11;

int readValue = 0;
double writeValue = 0;

const byte pushbutton = 9;
const byte piezo = 8;
const byte potentiometer = A0;

float actualDistance = 0;
float desiredDistance = 0;
int SensorValue =0;
int digitalValue =0;

void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(green1LED,OUTPUT);
pinMode(green2LED,OUTPUT);
pinMode(yellow1LED,OUTPUT);
pinMode(yellow2LED,OUTPUT);
pinMode(red1LED,OUTPUT);
pinMode(red2LED,OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(potentiometer, INPUT);
pinMode(pushbutton, INPUT);
//delay (10000);

attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed, CHANGE);

}

void loop()
{

long duration, distance;
int sensorValue;
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;

Serial.print(distance);
Serial.println(" cm Distance");
//Serial.println(sensorValue);

if(distance > 300)
// theese are my actual distance
{
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 1000, 100);
//tone( type of pin or pin number, frequency in hertz, duration in milliseconds)
}
if(distance > 250 && distance < 300)
{
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 900, 800);

}
if(distance > 200 && distance < 250)

{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 800, 1000);

}
if(distance < 150 && distance < 200)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
tone(piezo, 700, 600);

}
if(distance < 100 && distance < 150)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
tone(piezo, 600, 800);

}
if(distance < 50 && distance < 100)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
tone(piezo, 500, 1000);

}
delay(500);
attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed CHANGE);
}

NOT WORKING FROM HERE DOWN__________________________________________________

void flashRed()
{
Serial.println(" Desired Distance ");
SensorValue = analogRead(potentiometer);
desiredDistance = SensorValue /3.14;
actualDistance = echoPin;

if (desiredDistance > actualDistance)
{
analogWrite(desiredDistance, acceleraterPin);//green light on
Serial.print(desiredDistance);
Serial.println(" Desired Distance ");
}

else (desiredDistance < actualDistance);
{
analogWrite(breakPin, desiredDistance); //red light on
Serial.print(actualDistance);
Serial.println(" actualDistance ");
}


{

readValue = analogRead(potentiometer);
Serial.println(SensorValue);
delay(500);
writeValue =(8./1023.)*readValue;
analogWrite(redPin,writeValue);
analogWrite(greenPin,writeValue);
Serial.print(readValue);
Serial.println(writeValue);
delay(5000);
}

}

What does "NOT WORKING" actually mean?

It's doing something, but it may not be what you want it to do.

What is the desired behavior?

sclaire:
thank you for your comments guy this is not my homework

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code consistently for easier reading.

...R

#define trigPin 13
#define echoPin 12
const byte green1LED = 2;
const byte green2LED = 3;
const byte yellow1LED = 4;
const byte yellow2LED = 5;
const byte red1LED = 6;
const byte red2LED = 7;

const byte redPin = 10;
const byte greenPin = 11;


int readValue = 0;
double writeValue = 0;


const byte pushbutton = 9;
const byte piezo = 8;
const byte potentiometer = A0;


float actualDistance = 0;
float desiredDistance = 0; 
int SensorValue =0;
int digitalValue =0;

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green1LED,OUTPUT);
  pinMode(green2LED,OUTPUT);
  pinMode(yellow1LED,OUTPUT);
  pinMode(yellow2LED,OUTPUT);
  pinMode(red1LED,OUTPUT);
  pinMode(red2LED,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(potentiometer, INPUT);
  pinMode(pushbutton, INPUT);
  //delay (10000);
  
 attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed, CHANGE);
  
  
}

void loop()
{
  
  long duration, distance;
  int sensorValue;
  digitalWrite(trigPin, LOW);
  delay(2);
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2)/29.1;
  
  Serial.print(distance);
  Serial.println(" cm Distance");
  //Serial.println(sensorValue);

  if(distance > 300)
   // theese are my actual distance 
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    //tone(piezo, 1000, 100);
    //tone( type of pin or pin number, frequency in hertz, duration in milliseconds)
  }
  if(distance > 250 && distance < 300)
  {
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    //tone(piezo, 900, 800);
    
    
  }
 if(distance > 200 && distance < 250)
   
{
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    //tone(piezo, 800, 1000);
    
  }
 if(distance < 150 && distance < 200)
{
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    tone(piezo, 700, 600);
    
  }
if(distance < 100 && distance < 150)
{
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    digitalWrite(7,LOW);
    tone(piezo, 600, 800);
    
  }
if(distance < 50 && distance < 100)
{
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    digitalWrite(7,HIGH);
    tone(piezo, 500, 1000);
    
  }
   delay(500);
  attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed CHANGE);
}

  //Use the potentiometer to set the maximum (desired) distance
  //Once you have a value for the potentiometer, you will need
  //to convert it to a distance e.g. is your maximum distance is 
  //300, the potentiometer has a maximum of 1024, so you will need
  //to divide by 3
  
  //Based on how far the actualDistance is from the desiredDistance
  //If actualDistance is < increase brightness of Green LED
  //If actialDistance is > decrease brightness of Green LED and
  //increase brightness of RED lED
  //This brightness needs to be proportionate to the difference in distance
  //so do distance = desiredDistance - actualDistance
  //then see if this value will set your brightness correctly like
  //we did with the potentiometer example
  
  //Remember to use Serial.println to find out what values you are
  //producing.
  
  //Eventually you will need a button on an interrupt.  Then when
  //the  button is clicked this will accept the Potentiometer value


  void flashRed()
  {
         Serial.println(" Desired Distance ");
         SensorValue = analogRead(potentiometer);
         desiredDistance = SensorValue /3.14;
         actualDistance = echoPin;
         
       if (desiredDistance > actualDistance)
          {
           analogWrite(desiredDistance, acceleraterPin);//green light on
           Serial.print(desiredDistance);                    
           Serial.println(" Desired Distance ");
         } 
      
                           
          else (desiredDistance < actualDistance);
         {
           analogWrite(breakPin, desiredDistance);  //red light on 
           Serial.print(actualDistance);                    
           Serial.println(" actualDistance ");                      
        }



    {
         
  readValue = analogRead(potentiometer);
  Serial.println(SensorValue);
  delay(500);
  writeValue =(8./1023.)*readValue;
  analogWrite(redPin,writeValue);
  analogWrite(greenPin,writeValue);
  Serial.print(readValue);
  Serial.println(writeValue);
   delay(5000);
    }
    
  }

I am trying to hook up a potentiometer to two lights, that it will act as a dimmer on a distance on half the count will take on half the count on red and then on green,
the potentiometer will activate when the button is press to activate
its the section on void flashRed

thanks any help would be good

sclaire:
that it will act as a dimmer on a distance on half the count will take on half the count on red and then on green,

What is a "dimmer on a distance" ?

What does "half the count will take on half the count" mean ?

...R

sclaire:
thanks any help would be good

OK, now the second thing is that you need to go back and modify your original post - using the "More -> Modify" option below the right hand corner of your post - to mark up your code. It is not helpful to re-post the same code.

The first thing you need to do however, is to get rid of the "attachInterrupt" code since you clearly do not comprehend what an interrupt is.

Interrupts are a great trap for "newbies" for whom a common misunderstanding is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth! :astonished:

An interrupt is a mechanism for performing an action which can be executed in "no time at all" with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur.

Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds.

Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the delay() function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of microseconds, it will be handled in the loop().

So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.

An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.

sclaire:
I am trying to hook up a potentiometer to two lights, that it will act as a dimmer on a distance on half the count will take on half the count on red and then on green,
the potentiometer will activate when the button is press to activate
its the section on void flashRed

You assume there's a lot of knowledge and capability on this board. That is correct.

You also seem to assume mind reading abilities. This is not correct.

Assume this: The Arduino is a four-year-old that you're trying to explain this to. Statements like

that it will act as a dimmer on a distance on half the count will take on half the count on red and then on green,

are meaningless. Make a start by breaking your explanation into simple English sentences of one thought each.

Have you written on paper a proposed sequence of events to tell the Arduino what to do (pseudocode)?