What's the Best Way to Bounce A Variable Between 0 and 255

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 3358 Views - Last Post: 10 November 2016 - 09:37 AM

#31 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 08 November 2016 - 07:38 AM

Interesting Skydiver! Yeah I can see how it would consume more power. I'm more of a hardware geek than a software geek but in my days of overclocking learned quite a bit about how to save power (to save heat) by running better components wherever possible. I still think that they should have an option to re-enable the older style gradient title bars. I'm sure that all of the new telemetry isn't good for power usage either.


On the plus side: As an update, I got it working!!!

Thank you so much to everyone who posted to help me. I have bookmarked this page and will come back to all of your solutions. You guys are far better at programming than I am!! I can't wait to get on your levels. Here is what I ended up doing to solve this. I used two timers and this is working perfectly. It's a good solution for a n00b like myself. I tried all of your advice but was always plagued by compiler errors due to not having the background knowledge to port them into my solution. Like I said I will be bookmarking this page and coming back to it when I have more knowledge in the concepts required to port your code blocks.

#pragma endregion
	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
	{
		d--;
		textBox1->Text = d.ToString();
		if (d == 0)
		{
			timer2->Enabled = true;
			timer1->Enabled = false;
		}
	}
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
	{
		timer1->Enabled = false;
		timer2->Enabled = true;
	}
	private: System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e)
	{

		d++;
		textBox1->Text = d.ToString();
		if (d == 255)
		{
			timer2->Enabled = false;
			timer1->Enabled = true;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#32 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 19,818
  • Joined: 05-May 12

Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 08 November 2016 - 08:23 AM

Pseudo code for single timer solution:
On_Timer_Tick()
{
    d += delta;
    if (d < minimum || d > maximum)
    {
        delta *= -1;
        d = max(d, minimum);
        d = min(d, maximum);
    }
}



Moving this thread to C++/CLI since that is the language being used here.
Was This Post Helpful? 1
  • +
  • -

#33 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 08 November 2016 - 09:46 AM

Nice! thanks skydiver!
Was This Post Helpful? 0
  • +
  • -

#34 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 10 November 2016 - 09:37 AM

Just to give a quick update here's what I ended up doing with this:

https://sourceforge....wiring-help-v2/
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3