Simple circuit simulation

Hello, i am a noob in programming :o i just do electronics but i do use attiny 85 chips in my projects with simple code but can someone please help me with a little bit of simple code :confused:
I basicly want to emulate a capacitor and a diode so that you charge the cap and when it reaches a point of voltage the diode goes and fires a puls and the cap is empety again.

can i fully emulate this with a attiny chip?

Thanks alot! :slight_smile:

It sounds like you want the value of a variable to increase until it reaches a certain value then drop suddenly to zero when it reaches that value. What sort of time period are you thinking of ?

A simple for loop with a delay in it would seem to be the answer.

Thanks for the answer! I think it can just dorp to zero in an instand.
But it also has to send a pulse when it does that :wink: Thanks! If the answer is simple that would be awesome!

The CapacitiveSensor library may do something like you want. Just to get an idea how to code...

How long is the period during which the value ramps up and how long is the duration of the pulse ?

Here is a simple way to do it

const byte rampUpDelay = 50;
const byte pulseDuration = 1000;
const int triggerLevel = 150;
const byte pulsePin = 13;

void setup()
{
  Serial.begin(115200);
  pinMode(pulsePin, OUTPUT);
  digitalWrite(pulsePin, HIGH);  //start with output at a known state
}

void loop()
{
  for (int x = 0; x < triggerLevel; x ++)
  {
    Serial.print("Output : ");
    Serial.println(x);
    delay(rampUpDelay);
  }
  Serial.println("Pulse Start");
  digitalWrite(pulsePin, LOW);
  delay(pulseDuration);
  Serial.println("Pulse End");
  digitalWrite(pulsePin, HIGH);
}

It would be necessary to use millis() for timing if the program is doing anything else than what you describe.

Thanks for the reply! The duration of the puls can be 1 second but the duration of the ramp is endless uintill it reaches a certain value :slight_smile:

the duration of the ramp is endless

What influences the duration and how do you want to simulate it ? Your could use the input from a potentiometer to set the rate at which the output rises, a random number in a certain range or user input. Do you want to set the duration before the pulse triggers or the rate at which the output value rises towards the trigger value ? Both amount to the same thing but require programming differently.

Do you want to be able to change the value at which the pulse is triggered and how should that value be input ?

What should cause the output to start ramping up ? Maybe it starts again immediately after the pulse, on receipt of user input such as a button press or repeatedly with a delay after the pulse before the next ramp up of the output.

Do you want the output value to be printed, output as a PWM signal or have no actual output except for the pulse ? How about a number of LEDs in a line indicating progress towards the trigger value ?

All of the above is possible or you can simply have set values for any/all of the variables.

Thanks for the reply i really appriciate your help! But i think i did not described it good.

The input from the chip will get pulses from a few volts. My idear was that when there are enough pulses (voltage) build up an output spike will fire.

I diden't mean ramping up sorry :frowning:

Thanks, sorry :confused:

So, the program needs to count the input pulses on a pin and when it has received enough of them trigger an output pulse on a different pin. Is that right ?

Will all of the input pulses be of the same voltage and what will the input voltage range be ? Is the trigger voltage going to be fixed and, if so, what will it be ?

Thanks you understand it!! my fault i totaly explained it wrong.

Yeah the input voltage is the same as the output pulse because the inputs are output pulses from other chips maby we can do 2 volts? :confused:

Thanks! :slight_smile:

the input voltage is the same as the output pulse because the inputs are output pulses from other chips maby we can do 2 volts?

So the input voltage is fixed. How many input pulses must there be before the output is triggered ?

can we make a variable for that so that can be set? for a test we can do 5 Thanks! :slight_smile:

Here is a quick and dirty digital version which you can test using a switch and an LED.

const byte inputPin = A1;
const byte outputPin = 13;
byte inputCounter = 0;
const byte inputTriggerNumber = 5;
byte previousInputState = HIGH;
byte currentInputState;

void setup()
{
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);
  digitalWrite(outputPin, HIGH);  //set initial state of output to off
}

void loop()
{
  currentInputState = digitalRead(inputPin);
  if (currentInputState != previousInputState && currentInputState == LOW)  //input has become LOW
  {
    inputCounter++;
    Serial.println(inputCounter);
    if (inputCounter >= inputTriggerNumber)
    {
      digitalWrite(outputPin, LOW);  //turn on the pulse
      delay(2000);  //dirty (blocking) but OK if nothing else is going on in the program
      digitalWrite(outputPin, HIGH);  //turn off the pulse
      inputCounter = 0;  //reset the counter
    }
  }
  previousInputState = currentInputState;
}

Now it's time for you to do some work. Things that need doing include
1 - change the logic of the program to match your hardware and wiring
2 - change the input from digital to analogue. You will need to use analogRead() instead of digitalRead() and test for a reading above or below a threshold to establish when the input has exceeded the threshold voltage.
3 - change the output from digitalWrite() to analogWrite() to output a PWM signal to give a semblance of 2 volts.
3 - smooth the output to provide an actual 2 volt output instead of a PWM signal

Why not use the actual charge and discharge formulas for a capacitor?

Of which can be found here.
http://macao.communications.museum/eng/exhibition/secondfloor/moreinfo/2_3_5_ChargingCapacitor.html

Hey! because caps are very expensive here and then i need induvidual components wich would be bigger and more expensive.

Thanks for the reply trough :slight_smile:

The suggestion was to use the formula from the page linked to, not actual caps.

It would help if you could explain exactly what you are doing and why you need the program

I will be building a network of them so that one trigger the other :slight_smile:

Sorry for the dumb reply :confused: But it doesn't have to be that complicated just a simple something :slight_smile: i think what UKheli made is perfect i am gonna adjust it a little as he said :slight_smile:

Haduvokone:
I will be building a network of them so that one trigger the other :slight_smile:

How many are you intending to connect together and why ?

Because i want to build a neural net :wink: And alot i will slowly scale up to like 100